2

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
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Federi
  • 963
  • 8
  • 27
  • 38
  • 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
  • it certainly seems to me that you have your "root@blackbox" prompt back after the logout. – Jeff Schaller Jan 25 '16 at 17:01
  • Hi, the JeffSchaller is right. I thought that the "expect eof" was a command by itself and it didn't need for any extra values. I removed it and now it is working – Federi Jan 25 '16 at 17:07

1 Answers1

1

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?

glenn jackman
  • 85,964