ssh 期望带有变量的命令

ssh expect command with variables

我打算从文件中获取密码而不是将参数传递给脚本

set cmd1 {`cat passwdfile.txt | grep -w pj |  cut -d";" -f5`}
spawn ssh username@servername

expect "password: "
send "$cmd1\r"
expect "$ "
send "ps -ef |grep planning1\r"
expect "$ "
send "exit\r"

错误

    username@servername's password:
    Permission denied, please try again.

为什么不从文件中获取密码??

在 ssh 要求输入密码时,您无权访问 shell,因此反引号和所有其他内容将作为密码作为纯字符发送。

假设该文件存在于您的本地计算机上,请在生成之前获取密码:

set pw [exec grep -w pj passwdfile.txt | cut -d\; -f5]
spawn ssh ...
expect "password: "
send "$pw\r"

当然,以纯文本形式存储密码是非常不安全的。您应该设置 ssh 密钥以允许您无需输入密码即可登录。