期待新线

Expect new line

我正在尝试制作一个 bash/expect 脚本以通过 ssh 连接到 APC PDU(配电盘)并根据命令行参数远程重启插座​​。到目前为止我有这个:

#!/usr/bin/expect

#usage: pdu.sh user pass ipaddr outletNum

set user [lrange $argv 0 0]
set pass [lrange $argv 1 1]
set ipaddr [lrange $argv 2 2]
set outlet [lrange $argv 3 3]

spawn ssh $user@$ipaddr

sleep 60

expect "*?assword*"
send "$pass\n"
expect ">"

sleep 5

send "1\n"
expect ">"
send "2\n"
expect ">"
send "1\n"
expect ">"

send "$outlet\n"
expect ">"
send "1\n"
expect ">"
send "2\n"
send "yes\n"
expect ">"
send "1\n"
send "yes\n"

send "exit\n"

所以 SSH 的生成工作正常,它正确输入了密码,但是在我连接到 PDU 之后,它显示了选项界面,我的脚本发送了 12 而不是 1(输入)2 . 对不起,如果这令人困惑。这是它的作用:

它显示一个 12 而不是 1,然后是一个换行符,然后是一个 2。我特别让它在这一行的 1 之后发送一个换行符 send "1\n"

为什么 \n 在此示例中不起作用?谢谢!

预期使用 \r(回车 return)而不是 \n(UNIX 换行符)。 您也可以通过以下方式稍微简化您的期望脚本:

expect {
 ">" {send "1\r"; exp_continue}
 # Next expect token and response and so on 
}