我需要一个expect
脚本来等待已发出的命令完成,然后从telnet
注销。这是脚本:
spawn telnet host
send "username\r"
send "password\r"
sleep 3
send "custom_command\r"
while {???}
{
sleep 1
}
send "logout\r"
expect eof
我不知道如何发表短语的部分是???
。我基本上只需等待prompt
出现,一旦出现,脚本应该结束。我猜它应该是[gets line != "prompt>" ]
。
答案 0 :(得分:5)
在发送内容之前,你应该真正期待一些事情,以确保正确的时机。类似的东西:
exp_internal 1 ;# expect internal debugging. remove when not needed
spawn telnet host
expect "login: "
send "username\r"
expect "Password: "
send "password\r"
set prompt {\$ $} ;# this is a regular expression to match the *end* of
# your shell prompt. Adjust as required.
expect -re $prompt
send "custom_command\r"
expect -re $prompt
send "logout\r"
expect eof
答案 1 :(得分:4)
我尝试了expect命令,但它没有用,经过一些研究,试验和错误,我发现了以下内容:
expect "prompt>\r"
代替expect "prompt>"
expect "prompt>\r" {
set timeout -1
无限期等待提示,而不是等待10秒所以答案是:
spawn telnet host
send "username\r"
send "password\r"
sleep 3
set timeout -1
send "custom_command\r"
expect "prompt>\r" {
send "logout\r"
expect eof
}
答案 2 :(得分:2)
期望expect
command似乎合适。
像expect "prompt\n"
之类的东西,然后发送注销。
作为一个注释,如果这是一个普通的telnet系统,你通常应该在发送之前等待提示输入用户名和密码。请参阅how to automate telnet session using expect或expect script to automate telnet login