Search code examples
linuxexpect

Linux Expect waiting before exiting


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


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.