Linux Expect在退出前等待

9rnv2umw  于 2023-06-29  发布在  Linux
关注(0)|答案(2)|浏览(169)
#!/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的时候。我刚刚意识到,当我完全掌握了我的路由器配置。

vd2z7a6w

vd2z7a6w1#

问题已解决

#!/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的时候。我刚刚意识到,当我完全掌握了我的路由器配置。

ukqbszuj

ukqbszuj2#

使用'expect " "'代替'expect eof'
telnet会话中没有eof。即使没有关闭命令,它也会立即退出。不过,我还是不太确定“$”的期望值。应该会成功的可惜不是“>”而是“$”

相关问题