Even More TestLab Scripts

I doubt many home labs match hardware exactly with whatever the vendor has recommended. As a result most of us must script around or manually edit vendor configuration files. Here are some of my earlier scripts that I have shared that have been updated and a couple of new ones.

tl-checklist — TestLab CheckList
So that others will be able to understand the process, I have decided to walk you through the use of these scripts. The first one started out as a checklist of changes to make to each different device for every lab, but that was quickly replaced with a script. Funny thing about scripts like this, it should have probably been written in Perl but I did not expect it to grow to this size.

Each lab has its own directory of configuration files and this script works its way through each configuration file making changes as it goes. Here is an output of the usage example.

Usage: tl-checklist LAB-X
   tl-checklist LAB-1

I have tried to make the script idiot proof for my own sake. The first thing it does is make a copy of the directory so that a backup of the original exists. Then it puts a marker in the directory so that the script knows whether it has updated those configuration files before. If you have run the script on that directory it will fail gracefully.

if [[ $# -ne 1 ]]
then
        echo -e "Usage: ${0} LAB-X\n ${0} LAB-1\n"
        exit 1
elif [[ -e "/tftpboot/VOL-1/${LAB}/check-list" ]]
then
	echo -e "\n${0} has been run on this directory before."
	echo -e "Please remove /tftpboot/VOL-1/${LAB}/check-list."
	echo -e "And run the script again."
	exit 1
else
	touch "/tftpboot/VOL-1/${LAB}/check-list"

	if [[ ! (-d /tftpboot/VOL-1/${LAB}.0) ]]
	then
		cp -r "/tftpboot/VOL-1/${LAB}" "/tftpboot/VOL-1/${LAB}.0"
	fi
fi

Finally it loops through each device and makes changes that are need for each device to work properly with the other scripts and for the configuration to actually match my hardware connections.

for I in R1 R2 R4 R5 R6 R7 R9 Cat1 Cat2 Cat3 Cat4 BB1 BB2 BB3
do
	CONF="/tftpboot/VOL-1/${LAB}/INITIAL/${I}.txt"
	# The $$ on the end is just in case you run this script multiple times
	# the oldest file in an ls -tlr will be the true original.
	SAVE="/tftpboot/VOL-1/${LAB}/INITIAL/${I}.txt.$$"
	
	if [ -e $CONF ] 
	then
		mv $CONF $SAVE

		echo $I
	
		case $I in 
		R1 )
			sed 's/^boot.*//g
			s/^warm.*//g
			s/clock rate 2000000/clock rate 128000/g' $SAVE >$CONF
		;;
The rest is removed for brevity.

The long term reader will recognize that this script dovetails nicely with the previous testlab scripts. For instance, the backbone routers and catalyst switches are renamed to match the hostname expected in the upgrade scripts. It also uses the same command line switches so that it integrates with some of the earlier scripts.

tl-update-router — TestLab Update Router
The next script draws on tlue (testlab update expect) to go through and upgrade any number of switches or routers.

Usage: tl-update-router -l LAB-X -r Rx [dtn]
 -r Rx is required.
     If -r equals all, then the entire lab will have the specified configuration loaded.
     If -r equals routers, then the routers in the lab will have the specified configuration loaded.
     If -r equals switches, the switches in the lab will have the specified configuration loaded.
 -d load flash:def first.
 -t load flash:new from tftp
 -l LAB-X is required if tftp or new is specified.
 -n run configure replace flash:new
 -c configure the router from the console
tl-update-router -l LAB-1 -r all -dc

The final line of the usage example is how I normally run this script. This particular example tells the script to upgrade all of the switches and routers to the initial configuration for LAB-1.

tl-update-router -l LAB-1 -r all -dc

In doing so it will first run a configure replace command loading the flash:def file, then it will load the LAB-1 configuration file from the console. The reason I moved from configure replace of a tftp copied file onto flash was because I was not getting consistent results. If, however, I went back and pasted the switch or router configuration over the console input the configuration would take and as a result I just switched to having the script input the configuration over the console session.

tlue — TestLab Update Expect
The tlue script handle the interaction of the switches and routers through expect. A friend gave me a more thorough example but it seemed overkill for my purposes. By this time I know where I usually have problems, the serial interface of R2, but other than that this script works well.

This procedure is the one that sends the configuration over the console cable.

proc send_config {lab router} {

puts "send_config"
puts $lab
puts $router
global spawn_telnet
set spawn_id $spawn_telnet
expect "$router\#" {send "conf t\r\r"}
expect -re ".*Enter configuration.*" {send "\r"}

set file [open /tftpboot/VOL-1/$lab/INITIAL/$router.txt r]
foreach line [ split [read $file] "\n"] {
expect -re "$router.*" {send "$line \r"}
}
close $file
}
[/code]

If your hardware matches that of your vendor I would recommend running tl-update-route with the configure replace option, otherwise it is porbably best to configure it from the console.

tl -- TestLab
Finally, this is the last script I run which logs me into each every router or switch. It uses tlr for each terminal instance to log into the correct device, then changes the title accordingly. I do not use a "&" at the end of the command to execute in a subshell because it will not automatically log out of every session when terminating the window if you do. Regardless, control of the current terminal will be returned to you.

This entry was posted in Code, Routing. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s