问题
#!/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.
Here is the problem, when I run this code, it shows the correct output. But it doesn't exit afterwards, it's like waiting for an input. Like a timeout. But if I remove the line "EXPECT EOF". it terminates immediately. Can someone help me? I just started inLinux scripting and I've searched every topic on stackoverflow
EDIT: PROBLEM SOLVED
> > #!/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.
I got it. eof is when it it goes back to bash. I just realized that when I fully mastered my router config.
回答1:
PROBLEM SOLVED
#!/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.
I got it. eof is when it it goes back to bash. I just realized that when I fully mastered my router config.
回答2:
Instead of 'expect eof' use 'expect " "' you're welcome You see, there is no eof in a telnet session. Even without the close command, it will exit immediately. I'm still not sure about the expect "$" though. it should've worked. It's just sad it's not an ">" but an "$"
来源:https://stackoverflow.com/questions/23343223/linux-expect-waiting-before-exiting