【问题标题】:using set and expect in shell script在 shell 脚本中使用 set 和 expect
【发布时间】:2014-05-04 16:12:02
【问题描述】:

我正在尝试在 shell 脚本中使用期望 ssh 进入几个系统(从 test.txt 文件中读取)并在每个系统上执行命令。该脚本返回错误“无效的命令名称”。我在这里以不正确的方式使用 set 和 expect 吗?

#!/usr/bin/expect -f
set username "root"
set pass "mypassword"
set fd [open /home/test.txt r]
set host [read $fd]
foreach line $host {
   ssh -o StrictHostKeyChecking=no -n root@$host 'ls; pwd'
        expect "User:" { send "${username}\r" }
        expect "root's Password:" { send "${pass}\r" }
        expect eof
}

返回错误

./expect.sh
spawn ssh -o StrictHostKeyChecking=no -n root@10.1.1.1
10.1.1.2

 'ls
invalid command name "pwd'"
    while executing
"pwd' "
    ("foreach" body line 3)
    invoked from within
"foreach line $host {

【问题讨论】:

  • 谢谢。我尝试使用双引号并且不再发生错误。但是脚本仍然提示输入密码。返回的输出如下。 -- 生成 ssh -o StrictHostKeyChecking=no -n root@10.1.1.1 10.1.1.2 ls; pwd 警告:将“10.1.1.1 10.1.1.2”(RSA)永久添加到已知主机列表中。 root@10.1.1.1 10.1.1.2

标签: bash shell ssh set expect


【解决方案1】:

正如错误消息所暗示的,预计将; 解析为命令分隔符,并且无法处理pwd'。

这是因为语言中没有单引号字符串。

期望是tcl,必须用双引号:"ls; pwd"

【讨论】:

  • +1。需要强调的是,单引号在 Tcl 中没有任何意义。等价于 shell 的单引号是大括号:{ls; pwd}
猜你喜欢
  • 1970-01-01
  • 2022-10-19
  • 2022-10-24
  • 1970-01-01
  • 1970-01-01
  • 2021-02-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多