0

I'm new to this community as well as programming. I'm currently working on a an simple Expect script that that reads a file with a list of DNS names, SSH into a Cisco router, and does a simple "show ip int brief".

This list contains some hosts that are not reachable at the moment, so I'm trying to get the script to timeout that unreachable device but to continue with the rest of devices.

When I run the script, it is able to SSH to the first device and execute the "show" command. However, when it reaches the second device (which is unreachable), it hangs there for about 30 seconds and then terminates the script. I'm not sure what I'm doing wrong. Any assistance would be greatly appreciated.

#!/usr/bin/expect
#
#
set workingdir cisco/rtr
puts stdout "Enter TACACS Username:"
gets stdin tacuserid
system stty -echo
puts stdout "Enter TACACS password:"
gets stdin tacpswd
puts stdout "\nEnter enable password:"
gets stdin enabpswd
system stty echo
#
set RTR [open "$workingdir/IP-List.txt" r]
set timestamp [timestamp -format %Y-%m-%d_%H:%M]
#
while {[gets $RTR dnsname] != -1} {
if {[ string range $dnsname 0 0 ] != "#"} {
        send_user "The value of the router name is $dnsname\n"
        set timeout 10
        set count 0
        log_file -a -noappend $workingdir/session_$dnsname\_$timestamp.log
        send_log "### /START-SSH-SESSION/ IP: $dnsname @ [exec date] ###\n"
        spawn ssh -o StrictHostKeyChecking=no -l $tacuserid $dnsname
expect {

        "TACACS Password: " {send "$tacpswd\r"}
        timeout             {puts "$dnsname - failed to login"; wait;close;exp_continue}
        }
expect {
        {>}                 {send "enable\r"; send_user "on the second expect\n"}
                }
expect {
       {assword: }          {send "$enabpswd\r"}
       }
#
expect {
        "#"                 {send "show ip int brief\r"}
       }
#expect "#"
send "exit\r"
send_log "\n"
send_log "### /END-SSH-SESSION/ IP: $dnsname @ [exec date] ###\n"
log_file
        }
}
exit
PF75
  • 5
  • 2

1 Answers1

0

Your first expect is doing

expect {...
  timeout {puts "..."; wait; close; exp_continue}
}

This will match when the ssh takes over 10 seconds to connect to a host. When this matches it inevitably exits with an error (spawn id ... not open). This is because you wait for the command to end, close the spawn connection, then restart the same expect command.

You probably meant to use continue rather than exp_continue, in order to continue with the enclosing while loop.

meuh
  • 11,500
  • 2
  • 29
  • 45
  • Hi Meuh, I originally had the "continue" command and it wasn't working and that's why I decided to use the "exp_continue" command. I'm concern that the "continue" command is somehow not supported. When I enter the command on the VI editor, it doesn't get a special color code just like the other commands, such as, "expect", "timeout", etc. Is it possible that the command is not supported? – PF75 May 08 '20 at 17:53
  • You can read in `man expect` that *Actions such as `break` and `continue` cause control structures ... to behave in the usual way*. See the tcl tutorial [example](https://www.tcl.tk/man/tcl8.5/tutorial/Tcl9.html). – meuh May 08 '20 at 18:17
  • 1
    Meuh, I added the "continue" and experienced the same problem. However, I noticed that the "log_file" is supposed to close on every iteration. So, on the iteration where there was a time out, I needed to add the "Log_file". I did this to the "timeout" line and it worked. Thanks again for your help. – PF75 May 08 '20 at 20:51