【发布时间】: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