1

I have an autoexpect script. I modified it and it works great, however when it finish to run it hangs and I need to do ctrl+C in order to get my terminal

[..]
887VA#logout
Connection to 10.255.255.1 closed by remote host.
Connection to 10.255.255.1 closed.
root@blackbox:/etc/myscripts#

^Croot@blackbox:/etc/myscripts# ^C
root@blackbox:/etc/myscripts#

In the blank space above I pressed enter many times

Code

#!/usr/bin/expect -f

set force_conservative 0  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .1}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}

set timeout -1
spawn $env(SHELL)
match_max 100000


send -- "ssh f.olivieri@10.255.255.1\r"
expect -exact "Password: "
send -- "passwordhere!!!\r"
expect -exact "887VA#"
send -- "show interface vlan 2\r"
expect -exact "Vlan2 is up, line protocol is up \r"
send -- "logout\r"
expect eof
  • We can't read your mind. Show your code. – glenn jackman Jan 25 '16 at 13:57
  • Added in description. – Federi Jan 25 '16 at 14:51
  • 1
    I'm not an expect expert, but I wonder why you need to "expect eof" at the end? It looks like the 'logout' is being issued successfully; after that, you should be done? – Jeff Schaller Jan 25 '16 at 15:01
  • Is put as default from Autoexpect. @glenn jackman there is the code now. Do you have any suggestion? – Federi Jan 25 '16 at 16:01
  • 1
    Like @JeffSchaller says, you probably don't need to expect eof in that context - but if you do, don't you need to tell expect what to do when it gets it e.g. expect eof { exit }? – steeldriver Jan 25 '16 at 16:57
0

You spawn a shell, send an ssh command, do stuff, and logout of the ssh session. You never exit the shell.

I typically edit my autoexpect generated script to not spawn a shell but actually spawn what I want to do.

A short rewrite:

#!/usr/bin/expect -f
# default timeout is 10 seconds
spawn ssh f.olivieri@10.255.255.1
expect -exact "Password: "
send -- "passwordhere!!!\r"
expect -exact "887VA#"
send -- "show interface vlan 2\r"
expect -exact "Vlan2 is up, line protocol is up \r"
send -- "logout\r"
expect eof

Regarding the logic of your script: what should you do if the line protocol is not up?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.