Posts tagged EXPECT
Expect Scripts for ERS8600 Troubleshooting
1Whenever you contact Nortel, Cisco or Juniper support these days your going to need to spend some time and effort collecting a fair amount of information for the engineer assigned to your case.
I’ve written a very simple Expect script that will telnet into a Nortel Ethernet Routing Switch 8600 and issue a series of commands saving all the output so you can forward it to Nortel (or examine it yourself offline).
#!/usr/bin/expect -f # # Filename: /usr/local/etc/8600dump.exp # # Purpose: Dump technical information from Nortel Ethernet Routing Switch # via telneting to the device and issuing various "show" cmds. # The output will then be saved to the working directory using # a filename based on the switch name used to call the script. # # Language: Expect # # Author: Michael McNamara # # Date: May 6, 2003 # # Changes: # # Sept 29, 2006: cleaned up script/updated documentation # Dec 30, 2005: added command line arguments for portability # Mar 18, 2005: added file logging for troubleshooting and monitoring # May 20, 2003: fine tuned script removing a great many "expect" commands. # May 6, 2003: original Expect script generated from auto_expect # # Notes: # Command Line Reference; # ./8600dump.exp <switch> <username> <password> # # This Expect script was generated by autoexpect on Thu Aug 18 10:57:50 2005 # Expect and autoexpect were both written by Don Libes, NIST. # # 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 } } # # Declare Global Variables # set PATH "/usr/local/etc/" set TELNET "/usr/bin/telnet" # # Assign Command Line Variablbes # set SWITCH [lindex $argv 0] set USERNAME [lindex $argv 1] set PASSWD [lindex $argv 2] # # Time Date Stamp # set TODAY [timestamp -format %y%m%d ] set WEEKDAY [timestamp -format %a ] set DATE [timestamp -format %c ] set send_human {.1 .3 1 .05 2} ###################################################################### # proc usage # # Purpose: display the usage information to the enduser. ###################################################################### proc usage {} { send_user "\n" send_user "ERROR: command line paramaters incorrect\n" send_user "\n" send_user "usage: 8600dump.exp <switch> <username> <password>\n" send_user "\n" send_user " switch the DNS or IP address of switch \n" send_user " username the username for login to the switch \n" send_user " password the password for username\n" send_user "\n" send_user "\n" exit } ####################################################################### ####################################################################### # M A I N P R O G R A M ####################################################################### if {[llength $argv]!=3} usage log_file $PATH/$SWITCH.dump.log log_user 0 # Disable logging to STDOUT #log_user 1 # Enable logging to STDOUT # Useful information out to logfile send_log "******************************************************************\r\n" send_log "* STARTING LOGFILE FOR $SWITCH ON $DATE \r\n" send_log "******************************************************************\r\n" set timeout -1 spawn $TELNET $SWITCH match_max 100000 expect "Connected to" expect "Login: " send -- "$USERNAME\r" expect "Password: " send -- "$PASSWD\r" expect -re "\:.\#|> " # DATE send -- "date\r" expect -re "\:.\#|> " #################################################### # YOU CAN ADD AND REMOVE COMMANDS AS YOU SEE FIT #################################################### # CONFIG CLI MORE FALSE send -- "config cli more false\r" expect -re "\:.\#|> " # SHOW TECH send -- "show tech\r" expect -re "\:.\#|> " # SHOW CONFIG send -- "show config\r" expect -re "\:.\#|> " # SHOW SYS TOPO send -- "show sys topo\r" expect -re "\:.\#|> " # SHOW send -- "show ports error show-all\r" expect -re "\:.\#|> " # SHOW PORT ERROR MAIN #send -- "show port error main\r" #expect -re "\:.\#|> " # SHOW PORT ERROR EXT #send -- "show port error ext\r" #expect -re "\:.\#|> " # SHOW IP ROUTE INFO ALTERNATIVE #send -- "show ip route info alternative\r" #expect -re "\:.\#|> " # SHOW IP BGP SHOW-ALL #send -- "show ip bgp show-all\r" #expect -re "\:.\#|> " # DATE send -- "date\r" expect -re "\:.\#|> " send -- "logout\r" expect eof ####################################################################### # E N D P R O G R A M #######################################################################
You can also download the complete Expect script from my website here.
Occasionally you might have multiple switches that you’ll need to interrogate and for that I’ve written a quick and dirty little Bash shell script to loop through the FQDN of the switches calling the Expect script above.
#!/bin/sh
#
# Filename: /usr/local/etc/8600dump.sh
#
# Purpose: Dump technical information from Nortel Ethernet Routing Switch
# via telneting to the device and issuing various "show" cmds.
# The output will then be saving to the working directory using
# a filename based on the switch name used to call the script.
#
# Language: Bash Script
#
# Author: Michael McNamara
#
# Date: May 6, 2003
#
# Changes:
#
# Sept 29, 2006: cleaned up script/updated documentation
# Dec 30, 2005: added command line arguments for portability
# Mar 18, 2005: added file logging for troubleshooting and monitoring
# May 20, 2003: fine tuned script removing a great many "expect" commands.
# May 6, 2003: original Expect script generated from auto_expect
#
# Notes:
# Command Line Reference;
# ./8600dump.sh
#
# There are system and network specific variables below. Obviously the "PATH"
# to the location of the Bash script and supporting Expect script. The location
# of MUTT if email is used and most importantly the username and password to the
# Nortel Ethernet Routing Switch 8600. I would highly suggest using the ro (ReadOnly)
# account for all scripting purposes that are "read-only" in nature. The last most
# obvious piece is the list of switches that you'd like the script run against.
#
# Global Variables
PATH_TO=/usr/local/etc/mlh
DUMP=8600dump.exp
MAIL_LIST=''
PAGER_LIST=''
ERROR_FLAG=0
MAILEXE='/usr/bin/mutt'
MAILTXT='/tmp/mutt.txt'
LOCKFILE=/tmp/trace.lck
USERNAME=ro
PASSWORD=
SWITCHES='switch1.domain switch2.domain'
#############################################################################
# B E G I N M A I N
#############################################################################
for SWITCH in $SWITCHES
do
$PATH_TO/$DUMP $SWITCH $USERNAME $PASSWORD
$MAILEXE -s "ALERT: Dump Report for $SWITCH" $MAIL_LIST -a $PATH_TO/$SWITCH.trace.log < $MAILTXT
done
exit
#############################################################################
# E N D M A I N
#############################################################################You can also download the complete shell script here.
The use of Expect really helps save me a lot of time and it speeds up the troubleshooting process with the vendor.
Cheers!
Expect Script – Daylight Saving Time
5I recently received a message from someone looking for someway to automated the re-configuration of over 100 switches with the correct Daylight Saving Time configuration. I explained to the person that the best long term solution would probably be to use the SNMP MIB but a quick and dirty solution might be to use Expect and call it from a Bash script looping over all the switches that needed to be re-configured. In short Expect is a scripting language that mimics user input at a TTY. The Except script is written to issue a set of commands, as if a human were typing them, and expects various responses.
The script I wrote below only support a limited number of switches. If you have a particular switch you’re welcome to modify the script to support that particular switch. The script will attempt to determine if the switch is running the software that has the features we’re looking to implement. I didn’t have a whole lot of time to test so buyer beware!
Here’s the expect script that I authored;
#!/usr/bin/expect -f
#
##############################################################################
#
# Filename: /usr/local/etc/set-nortel-timezone.exp
#
# Purpose: Expect script designed to telnet into Nortel Ethernet Switches
# and execute the CLI commands to confgure the appropriate timezone
# information, including Day Light Saving time.
#
# Switches: Ethernet Switch 460 v3.7.x
# Ethernet Switch 470 v3.7.x
# Ethernet Switch 4500 v5.2.x
# Ethernet Switch 5500 v5.1.x
#
# Author: Michael McNamara
#
# Date: June 1, 2008
#
# Version: 1.1
#
# Changes:
#
# June 8, 2008 (M.McNamara)
# - added documentation and ARGV command line checks
# June 14, 2008 (M.McNamara)
# - added check for switch version and exit if v3.6 switch software
# - added check for Username introduced in v3.7 switch software
#
#
##############################################################################
#
# This Expect script was generated by autoexpect on Wed Jul 27 17:25:28 2005
# Expect and autoexpect were both written by Don Libes, NIST.
#
set force_conservative 1 ;# 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
}
}
if {[llength $argv] != 2} {
puts "usage: set-nortel-timezone.exp < SWITCH > < PASSWORD >>"
exit 1
}
#
set PATH "/usr/local/etc/"
set TELNET "/usr/bin/telnet"
set SWITCH [lindex $argv 0]
set PASSWORD [lindex $argv 1]
set TODAY [timestamp -format %y%m%d ]
set WEEKDAY [timestamp -format %a ]
set DATE [timestamp -format %c ]
set send_human {.1 .3 1 .05 2}
#log_file $PATH/$SWITCH.expect.log
log_file /usr/local/etc/password.expect.log
log_user 0 # Disable logging to STDOUT
#log_user 1 # Enable logging to STDOUT
set timeout 10
spawn $TELNET $SWITCH
match_max 100000
expect "Trying"
expect {
"Connected" {
expect "SW:v3.6" {
send_log "\n\nThis version of software doesn't support the CLI commands!\n"
send_user "\n\nThis version of software doesn't support the CLI commands!\n"
exit 1
}
sleep 1
send -- ""
}
Timeout {
send_log "We're unable to connect to the switch $SWITCH"
send_user "We're unable to connect to the switch $SWITCH"
exit 1;
}
}
expect {
"Username" {
send -- "RW\r"
}
}
expect "Enter Password"
send -- "$PASSWORD\r"
expect {
"Main Menu" {
}
"Incorrect Password" {
send_log "$SWITCH : Incorrect Password"
exit 1
}
"Incorrect Credentials" {
send_log "$SWITCH: Incorrect Credentials"
exit 1
}
}
sleep 1
# Let's get into the CLI interface from the menu prompts
send -- "C"
# Depending on the version of software we sometimes need a CR/LF
send -- "\r"
sleep 1
# Let's wait for the CLI prompt which includes the #
expect "#"
send -- "config term\r"
send -- "clock time-zone EST -5\r"
send -- "clock summer-time EDT date 9 Mar 2008 2:00 2 Nov 2008 2:00 +60\r"
send -- "exit\r"
send -- "logout\r"
expect eofYou can download the entire Expect script from this URL; set-nortel-timezone.exp.
The command line arguments are fairly straight forward;
usage: set-nortel-timezone.exp <SWITCH> <PASSWORD>
Where the SWITCH is the fully qualified domain name (FQDN) or the IP address of the switch in question and the PASSWORD is the Read-Write password for the switch.
If you had hundreds of switches to reconfigure you could wrap this Except script in a Bash shell script similar to the following;
#!/bin/bash # ##################################################################### # # Language: Bash Shell Script # # Filename: /usr/local/etc/set-nortel-timezone.sh # # Purpose: This script will kickoff the Expect script that will # configure the Daylight Saving Time features for each switch # # Author: Michael McNamara # # Date: June 1, 2008 # # Version: 1.0 # # Changes: # # June 10, 2006 (M.McNamara) # - added remote sites into shell script processing # ##################################################################### # # Variables PATH_TO=/usr/local/etc UPGRADE=set-nortel-timezone.exp MAIL_LIST='' PAGER_LIST='' ERROR_FLAG=0 MAILEXE='/usr/bin/mutt' LOCKFILE=/tmp/trace.lck # Check paramaters if [ "$#" != 2 ] then echo "Usage: `basename $0` <password>" exit 1 fi PASSWORD=$1 ##################################################################### ##################################################################### # YOU SHOULD EDIT THE "SWITCHES" VARIABLE BELOW TO INCLUDE ALL THE # SWITCHES THAT YOU WISH TO HAVE THE EXPECT SCRIPT RUN AGAINST ##################################################################### ##################################################################### SWITCHES='sw1-5520.acme.org sw2-5520.acme.org sw3-5520.acme.org' for SWITCH in $SWITCHES do $PATH_TO/$UPGRADE $SWITCH $PASSWORD done exit
You can download the Bash shell script from this URL; set-nortel-timezone.sh.
I’ve only tested this on CentOS v5.2 but it should work on any Linux host with Expect installed although you may need to modify the path locations.
Cheers!


