
這可能是一個簡單的問題,但是有一個簡單的期望腳本,我添加了可執行位,似乎忽略了 #!/usr/bin/expect 解釋器行。此外,似乎還沒有設定變量,因為當我回顯它們時,它們是空白的...
#!/usr/bin/expect -f
set device "1.1.1.1"
set user "testuser"
spawn ssh $user@$device
echo $device
echo $user
ls -lh
-rwxr-xr-x root root testexpect.exp
感謝您的幫助社區!
PS 我正在運行 Debian Wheezy,透過 apt-get install Expect 安裝了 Expect...謝謝
答案1
Expect 是基於 Tcl 語言,因此您不應該使用 bash 'echo' - 您應該使用 'puts' 在螢幕上列印一些內容:
#!/usr/bin/expect -f
set device "1.1.1.1"
set user "testuser"
spawn ssh $user@$device
puts $device
puts $user
你會得到這樣的結果:
$ ./test.exp
spawn ssh [email protected]
1.1.1.1
testuser
答案2
錯誤訊息表示您正在使用明確解釋器執行命令,例如:
bash ./testexpect.exp
這將忽略“#!”線。無論啟動腳本什麼,都只需命名檔案(或明確地說expect ./testexpect.exp
)。如果你的 #!線路很糟糕,你會得到/usr/bin/expect: bad interpreter: ...