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

时间:2018-12-11 10:28:57

标签: linux bash shell expect heredoc

我们输入的密码为bt67nJuse3?{]=_0!\`fr,./&##@(引号除外)。该密码包含诸如{`/.!之类的字符,可能需要转义。许多组合都无济于事,并且在花费大量时间来解决此问题后,以下代码段无法正常工作。

已经尝试:

  • 'bt67nJuse3?{]=_0!`fr,./&##@'"\r"
  • "bt67nJuse3?{]=_0!`fr,./&##@\r"
  • "bt67nJuse3?{]=_0"'!'"`fr,./&##@\r"
  • 'bt67nJuse3?{]=_0!`fr,./&##@\r'
  • "bt67nJuse3?{]=_0!\`fr,./&##@\r"
  • "bt67nJuse3?{]=_0!\`fr,./&##@\r"

代码开始:

#!/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

2 个答案:

答案 0 :(得分:0)

将其作为单独的文件并使用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
}

并使用“期望文件名”有效!!!哇,天哪

答案 1 :(得分:-1)

第一行

/usr/bin/expect << EOF

必须更改为

/usr/bin/expect << 'EOF'

主要问题是由于bash如何在此处处理文档

      <<[-]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 `.