Linux在退出之前期待等待

时间:2014-04-28 14:03:38

标签: linux expect

#!/usr/bin/expect
# Test expect script to telnet.

spawn telnet 192.168.1.1
expect "Username: "
send "adminpldt\r"
expect "Password: "
send "password\r"
expect "$"
send "show lan\r"
send "show\r"
expect eof
close
# end of expect script.

这是问题,当我运行此代码时,它显示正确的输出。但它之后不会退出,就像等待输入一样。像超时一样。但是,如果我删除该行 “期待EOF”。它会立即终止。有人能帮我吗?我刚开始使用Linux脚本编写,我在stackoverflow上搜索了每个主题

编辑:问题已解决

>     >     #!/usr/bin/expect
>     >     # Test expect script to telnet.
>     >     
>     >     spawn telnet 192.168.1.1
>     >     expect "Username: "
>     >     send "adminpldt\r"
>     >     expect "Password: "
>     >     send "password\r"
>     >     expect "$"
>     >     send "sh\r"
>     >     send "config\r"
>     >     send "macc interface lan1 [lindex $argv 0]\r"
>     >     send "macc interface lan2 [lindex $argv 0]\r"
>     >     send "macc interface lan3 [lindex $argv 0]\r"
>     >     send "macc interface lan4 [lindex $argv 0]\r"
>     >     send "macc interface wlan [lindex $argv 0]\r"
>     >     send "exit\r"
>     >     send "exit\r"
>     >     expect eof
>     >     # end of expect script.

我明白了。 eof就是它回到bash的时候。我刚刚意识到当我完全掌握了我的路由器配置时。

2 个答案:

答案 0 :(得分:1)

问题已解决

#!/usr/bin/expect
# Test expect script to telnet.

spawn telnet 192.168.1.1
expect "Username: "
send "adminpldt\r"
expect "Password: "
send "password\r"
expect "$"
send "sh\r"
send "config\r"
send "macc interface lan1 [lindex $argv 0]\r"
send "macc interface lan2 [lindex $argv 0]\r"
send "macc interface lan3 [lindex $argv 0]\r"
send "macc interface lan4 [lindex $argv 0]\r"
send "macc interface wlan [lindex $argv 0]\r"
send "exit\r"
send "exit\r"
expect eof
# end of expect script.

我明白了。 eof就是它回到bash的时候。我刚刚意识到当我完全掌握了我的路由器配置时。

答案 1 :(得分:0)

而不是 '期待eof' 使用'期待" "' 你很受欢迎 你看,telnet会话中没有eof。即使没有关闭命令,它也会立即退出。我还不确定期望" $"虽然。它应该有效。很遗憾它不是">"但是" $"