Get Serial Numbers with Expect
From NippAero
A quick way to get the serial number from a routers. Outputs the router name and serial number to a CSV file.
This can easily be wrapped in a simple shell script to loop through a list of routers.
#!/usr/bin/expect -f
#
# useage: getsn.exp hostname
# check to see if we got the hostname
if { [llength $argv] != 1} {
puts " "
puts "getsn.exp hostname"
puts " "
puts " "
exit
}
set router [lindex $argv 0]
set password "password"
set user "username"
set logpath "/home/log"
#---------------------
# Set up logging
#---------------------
proc data_log {mnu} {
set file [open /home/log/routersn.log a]
puts $file "$mnu"
}
#----------------------
# SSH to the router
#----------------------
log_user 0
spawn ssh $user@$router
#----------------------
# enter the login password
#----------------------
expect {*password:*} {}\
timeout {timedout waiting for login prompt}
send ${password}\r
expect {password:*} {send_user Bad password\n; exit} \
{#} {} \
timeout {timedout waiting for login prompt}
#----------------------
# Now reset the screen size to get rid of the page prompt
#----------------------
send "term len 0\r"
expect {*#} {} \
timeout {timedout waiting for login prompt}
#----------------------
# Clear screen and issue "sh run" command
#----------------------
log_user 1
send_user [exec /usr/bin/clear]
send "show ver\r"
expect -re "(.*)(Processor board ID)(.*)( )(.*)"
set serial [lindex $expect_out(3,string) 0]
#----------------------
# Write data to the log file in CSV format
#----------------------
data_log "$router,$serial"
send exit\r
expect eof
Categories: Network Management | Expect | Shell | Script | Cisco
