#!/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 时。当我完全掌握了路由器配置后,我才意识到这一点。
最佳答案
问题已解决
#!/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 时。当我完全掌握了路由器配置后,我才意识到这一点。
关于Linux 退出前等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23343223/