Expect / Shell 脚本中密码中的非法字符

Illegal Characters in Password in Expect / Shell Script

我们的密码是 bt67nJuse3?{]=_0!\`fr,./&##@(引号除外)。此密码包含 {`/.! 等可能需要转义的字符。尝试了很多组合都无济于事,在花了很多时间试图解决这个问题后,以下代码片段不起作用。

已经尝试过:


代码开始:

#!/bin/bash
/usr/bin/expect << EOF
spawn scp user111@servername.domain.com:/home/path1/test1.log /home/path2/
expect {
    " password: " { send 'bt67nJuse3?{]=_0!`fr,./&##@'"\r";  exp_continue }
    eof
}
... some code ...
EOF

如何将此密码输入 expect

第一行

/usr/bin/expect << EOF

必须改为

/usr/bin/expect << 'EOF'

主要问题是bash如何处理here-doc

      <<[-]word
                  here-document
          delimiter

  ...  If any characters in word are quoted, the delimiter is
  the result of quote removal on word, and the lines in the here-document are not expanded.  If word is unquoted, all lines of the here-document are subjected to parameter  expansion,
   command substitution, and arithmetic expansion, the character sequence \<newline> is ignored, and \ must be used to quote the characters \, $, and `.

将其作为一个单独的文件并使用 expect 命令工作:

set password "bt67nJuse3?{]=_0!\`fr,./&##@";
spawn scp user111@servername.domain.com:/home/path1/test1.log /home/path2/
expect {
    " password: " { send "$password\r";  exp_continue }
    eof
}

和使用 "expect fileName" 成功了!!!哇天啊