0
我想在shell腳本中使用expect並在每個腳本上執行命令來嘗試ssh進入幾個系統(從test.txt文件讀取)。該腳本返回一個錯誤「無效的命令名稱」。我在這裏使用set和期望的方式不正確嗎?在shell腳本中使用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 [email protected]$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 [email protected]
10.1.1.2
'ls
invalid command name "pwd'"
while executing
"pwd' "
("foreach" body line 3)
invoked from within
"foreach line $host {
謝謝。我用雙引號試過,錯誤不再發生..但腳本仍然提示輸入密碼..返回的輸出如下。 - spawn ssh -o StrictHostKeyChecking = no -n [email protected] 10.1.1.2 ls; pwd 警告:將'10.1.1.1.1 10.1.1.2 '(RSA)永久添加到已知主機的列表中。 [email protected] 10.1.1.2 – user3331975