Wednesday, January 3, 2018

Expect Script for collecting info and logs (fails when logs are too long)

Hi there,

I'm writing an expect script to hasten collecting vital info for troubleshooting purpose whenever a fault occurs.

Basically it's going to consists of show logging, show interface status, show ip interface brief and other commonly used commands for tshooting common and basic device faults, saving time and effort logging in manually and checking things one by one.

I've came up with a problem though, whereby if the "show logging" buffer comes up longer than a certain extent, my script fails to continue and just hangs at the next prompt.... occasionally it will manage to execute 1 more command but then hangs there. I've tried adding sleep or wait but it doesn't seem to help.

Below is my script, maybe you guys can see something I can't:

#!/usr/bin/expect # Receive Variables #~~~~~~~~~~~~~~~~~~~ set ipaddress [lindex $argv 0] # set username [lindex $argv 1] # set rsapin [lindex $argv 2] # set enable [lindex $argv 3] # set configfile [lindex $argv 4] set timeout 10 set outputfile [open expectlog.log a] set username "<username>" set rsapin "<PIN>" set enable "<enable>" #~~~~~~~~~~~~~~~~~~~~~~~~ # PROC Gather Information #~~~~~~~~~~~~~~~~~~~~~~~~ proc getinfo {} { send "terminal length 0\r" expect "*#$" {send "show version | inc uptime\r\r"} expect "*#$" {send "show log\r\r"} expect { "*rt0?#$" { send_user "Device type - Router" expect "*rt0?#$" {send "show ip interf brief\r"} expect "*rt0?#$" {send "show interf desc\r"} expect "*rt0?#$" {send "show dmvpn\r"} } "*sw0?#$" { send_user "Device type - Switch" expect "*sw0?#$" {send "show interf status\r"} expect "*sw0?#$" {send "show interf desc\r"} } default { send_user "Unable to determine prompt, check getinfo{} proc" } } } # Create Log file #~~~~~~~~~~~~~~~~~ log_file -a expect.log # Begin SSH #~~~~~~~~~~~ sleep 2 send_user "\n" send_user "Attempting SSH >> $ipaddress @ [exec date]\n" puts -nonewline $outputfile $ipaddress spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $username@$ipaddress expect { timeout { send_user "\nTimeout Exceeded\n" puts $outputfile " - Timeout Exceeded (10)" exit 10 } eof { send_user "\Connection Failed\n" puts $outputfile " - Connection Failed (20)" exit 20 } "*" {} } # User Access Verification and Enable Password #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #expect "?sername: $" {send "$username\r"} expect { "Enter PASSCODE: $" {send "$rsapin"} "username@*'s password: $" {send "$rsapin"} } # Acquire Token key from user #~~~~~~~~~~~~~~~~~~~~~~~~~~~~ interact -o "\r" return # Verify authenticated, Enable Password #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expect { default { send_user "\nTimeout or connection lost.\n" puts $outputfile " - Timeout or connectiviy lost (11)" exit 11 } "*failed" { send_user "\nAccess failed, possible authentication issue.\nIf problem persists please try again later.\n" puts $outputfile " - Authentication Failure (1)" exit 1 } "*denied" { send_user "\nPlease check username/password\n" puts $outputfile " - Authentication Failure (2)" exit 2 } "*>$" { send "enable\n" expect "?assword: $" send "$enable\n" expect "*#$" } "*#$" {} } getinfo expect -timeout 5 "*#$" {send "exit\n"} puts $outputfile " - Done, quitting without error (0)" close $outputfile exit 0 


No comments:

Post a Comment