Question

#!/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.

Was it helpful?

Solution

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.

OTHER TIPS

Instead of 'expect eof', use 'expect " "'.

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 "$"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top