Introduction
In this article we are going to create expect script that backup the configuration of Cisco devices in your network every midnight 😉
For this setup we need Linux machine (Ubuntu is my favorite), tftp-server, expect shell and some bash here and there
Ok lets do this !
tftp-Server:
I assume you already have your linux machine up and running (like I said I prefer Ubuntu)
First install tftp-server
sudo apt-get install tftpd-hpa
Then, edit the tftpd-hpa configuration file.
sudo vim /etc/default/tftpd-hpa
and change the line ..
TFTP_OPTIONS=”–secure”
to
TFTP_OPTIONS=”-s -c”
change the permission of /var/lib/tftpboot
sudo chown -R tftp /var/lib/tftpboot
Finally restart the service
sudo service tftpd-hpa restart
I didn’t expect that !
Ok, first of all install expect shell
sudo apt-get install expect
Next, expect script we are goning to make “expects” from you IP addresses and passwords to work. so you have to create a file (devices.txt) contain your devices
10.10.10.1
10.10.10.2
10.10.10.3
core-sw01
core-sw02
Next, create a shell script (config-sw.sh) that execute our expect script (tricky huh !)
#!/bin/bash
# Feed the expect script a device list & the collected passwords
for device in `cat devices.txt`; do
./exp-config-sw.exp $device ;
done
and last but not least the “expect” script, here is the whole file and we will go through it segment by segment
#!/usr/bin/expect -f
#################### First Segment ########################
set hostname [lindex $argv 0]
set username “admin”
set password “password”
# Log results
log_file -a ~/results.log
#################### Second Segment ########################
# Announce which device we are working on and at what time
send_user “\n”
send_user “>>>>> Working on $hostname @ [exec dateo] <<<<<\n”
send_user “\n”
spawn telnet $hostname
expect {
timeout { send_user “\nTimeout Exceeded – Check Host\n”; exit 1 }
eof { send_user “\nTelnet Connection To $hostname Failed\n”; exit 1 }
“*ser*” {
send “$username\n”
expect “*assword:” {
send “$password\n”
expect “*#”
}
}
}
#################### Third Segment ########################
send “copy running-config tftp://10.10.10.254/$hostname-[exec dateo].txt \n”
expect “Source”
send “\n”
expect “Address”
send “\n”
expect “Destination”
send “\n”
expect “#”
send “exit\n”
expect “:~\$”exit
The #First Segment# set the IP address, username and password
the IP address ($hostname ) is received from the bash shell script. a log file is created to log what’s going on
The #Second Segment# show the user what the script is doing by printing every command and the output of that command
- spawn telnet $hostname #establish a telnet session to $hostname
- if timeout occurred notify user
- expect user name and password pattern and send each promptly
The #Third Segment# copy the configuration to a tftp-server the destination file name is the $hostname-dateo
dateo is a modified version of date with spaces substituted with ‘-‘. all you have to do is create a file dateo.sh and copy it to /usr/ bin/
#!/bin/bash
date | sed ‘s/ /-/g’
Finally cron it
#sudo crontab -e
0 0 * * * /bin/config-sw.sh
References
https://help.ubuntu.com/community/TFTP