在 bash 中使用 expect 执行从文件传递密码的命令

在 bash 中使用 expect 执行从文件传递密码的命令

有人能帮我完成以下请求吗?我正在尝试从文件发送密码以在远程机器上执行脚本,但它给出了错误

Password: can't read "sys_pw": no such variable while executing "send "echo $sys_pw\r""

这是我的代码..

#!/bin/bash

sys_pw=`cat /home/admin/scrp/sys.pw`

expect -c 'spawn ssh admin@<servername> -o StrictHostKeyChecking=no "df -Ph"; expect "Password:"; send "$sys_pw\r"; interact'

答案1

它给出错误

Password: can't read "sys_pw": no such variable while executing "send "echo $sys_pw\r""

你的脚本坏了:

$ shellcheck myscript
 
Line 3:
sys_pw=`cat /home/admin/scrp/sys.pw`
^-- SC2034 (warning): sys_pw appears unused. Verify use (or export if used externally).
       ^-- SC2006 (style): Use $(...) notation instead of legacy backticks `...`.

Did you mean: (apply this, apply all SC2006)
sys_pw=$(cat /home/admin/scrp/sys.pw)
 
Line 5:
expect -c 'spawn ssh admin@<servername> -o StrictHostKeyChecking=no "df -Ph"; expect "Password:"; send "$sys_pw\r"; interact'
          ^-- SC2016 (info): Expressions don't expand in single quotes, use double quotes for that.

$ 

来源:ShellCheck – shell脚本分析工具

相关内容