【问题标题】:Illegal Characters in Password in Expect / Shell ScriptExpect / Shell 脚本中密码中的非法字符
【发布时间】:2019-05-12 07:48:47
【问题描述】:

我们的密码是bt67nJuse3?{]=_0!\`fr,./&##@(引号除外)。此密码包含可能需要转义的字符,例如{`/.!。尝试了很多组合都无济于事,在花费大量时间尝试解决此问题后,以下 sn-p 无法正常工作。

已经试过了:

  • '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

【问题讨论】:

  • 用 \. 转义反引号
  • 无法使用“bt67nJuse3?{]=_0!`fr,./#@\r”在执行“expect {”密码:“{发送“bt67nJuse3?{ ]=_0!`fr,./#@\r"; exp_continue } eof } "
  • 尝试先声明密码,设置密码 "bt67nJuse3?{]=_0!`fr,./#@";然后发送“$password\r”
  • 问题可能是由于引用缺少 arround bash here-doc delimiter EOF 比较 cat &lt;&lt;'EOF'cat &lt;&lt;EOF 在第一个变量中没有展开,而在第二个变量中
  • 我试过这个并使用`表示“" and it pasted the wrong password set password "bt67nJuse3?{]=_0!fr,./#@”;发送“$password\r”

标签: linux bash shell expect heredoc


【解决方案1】:

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

并且使用“期望文件名”有效!!!哇哦

【讨论】:

    【解决方案2】:

    第一行

    /usr/bin/expect << EOF
    

    必须改为

    /usr/bin/expect << 'EOF'
    

    主要问题是由于这里 bash 的处理方式-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 `.
    

    【讨论】:

    • @BejoyM,请注意单引号在 Tcl 中没有任何意义:它们只是普通字符。在 Tcl 中使用 {braces} -- 它们与 shell 单引号具有相同的目的:对单词进行分组而不进行插值。
    猜你喜欢
    • 2013-07-29
    • 2018-07-20
    • 1970-01-01
    • 2022-10-19
    • 2014-05-04
    • 1970-01-01
    • 2018-07-07
    • 2013-06-14
    相关资源
    最近更新 更多