cygwin - Expect while loop dies after 29th iteration -
i'm using expect script within cygwin. reads 2 input files: 1 list of network devices full hostname, , other list of commands run on devices while logging output. works until completing 29th device. when spawn command executes on 30th device, output:
send: spawn id exp65 not open while executing "send -s "myuserid\r"" ("while" body line 30) invoked within "while {[gets $switches host] >= 0} { set hostname [string trimright $host] ;# rid of trailing whitespace if {[string length $hostname] == 0} {..." (file "./getna-lab.exp" line 37)
to rule out issue cygwin, wanted test mac used expect, can't anymore (devices secured , available via windows virtual desktops, hence cygwin.) @ first thought because "exit" telnet session wasn't working , remaining open, that's not it; tried adding "exit" command list file executes.
the script , other file contents listed below. assistance.
#!/usr/bin/expect set timeout 600 log_user 0 match_max 100000 set expect_out(buffer) {} set switchlist [lindex $argv 0] set commandlist [lindex $argv 1] # # open switch list file # if [catch {open "$switchlist" "r"} switches] { puts stderr "$cr$switches$ce\n" exit 1 } # # verify command list file opens ok before logon, etc. # if [catch {open "$commandlist" "r"} commands] { puts stderr "$cr$commands$ce\n" exit 1 } close $commands # # enable trace logging # trace variable expect_out(buffer) w log_by_tracing # # loop each device in switch list, looping each command in command list # while {[gets $switches host] >= 0} { set hostname [string trimright $host] ;# rid of trailing whitespace if {[string length $hostname] == 0} { ;# leave loop if blank line detected break } regexp {^([^\..]+)(\..*$)} $hostname domain hostshort ;# peel off domain name send_user "\n$hostname:\nworking...\n" if [catch {open "$commandlist" "r"} commands] { puts stderr "$cr$commands$ce\n" exit 1 } log_file -noappend -a "$hostshort.txt" spawn telnet $hostname expect \ { "sername:" {} "ogin:" {} "assword:" {send -s "\r" expect \ { "ogin:" {} "sername:" {} } } timeout { send_user "timed out on device $hostshort logon.\n" break } } send -s "myuserid\r" expect { "assword:" {} timeout { send_user "timed out on device $hostshort logon.\n" break } } send -s "mypassword\r" expect { "$hostshort" {} timeout { send_user "timed out on device $hostshort logon.\n" break } } while {[gets $commands command] >= 0} { send -s "$command\r" expect { "$hostshort#" {} timeout {send_user "timed out waiting $hostshort # prompt.\n"} } } send -s "exit\r" log_file } close $switches close $commands
i typically use switchlist.txt file looks this:
switch1.domainname.com switch2.domainname.com
i use file called commands.txt lists commands this:
show interface status sh run
the script executed via
# ./get-stuff.exp switchlist.txt commands.txt
i suspect may running out of (something) not closing spawned processes inside loop. try replacing contents of loop this:
spawn telnet $hostname expect { "sername:" { send -s "$myuserid\r"; exp_continue } "assword:" { send -s "mypassword\r"; exp_continue } timeout { send_user "timed out on device $hostshort logon.\n" continue ;# next host } "$hostshort" } while {[gets $commands command] >= 0} { send -s "$command\r" expect { "$hostshort#" {} timeout { send_user "timed out waiting $hostshort # prompt.\n" continue ;# next host } } } send -s "exit\r" expect eof wait exp_close
Comments
Post a Comment