Using Cisco TCL To Automate Lab Resets

I have been logging a considerable amount of time behind the Cisco IOS CLI lately while working towards my CCIE R/S lab. I am lucky enough to have access to my own lab hardware for study but one of the things I identified fairly quickly was that resetting my devices between labs was taking a considerable amount of time. This time is non-productive as it isn’t being applied to learning new technologies but rather to the “process” of getting set up. I needed to find a way to quickly get all my devices back to a base configuration without expending a lot of time and effort. Here is what I came up with utilizing the TCL scripting engine on Cisco devices.

Step 1 > Setting up your baseline configuration

This is pretty much exactly what it sounds like. Starting from a completely default configuration, apply whatever items you would like to consistently have across all devices and all lab scenarios. This might be something as simple as “no ip domain-lookup” to save time when you fat finger a command, hostnames, or possibly login credentials for console access. Try to keep this as basic as possible to avoid conflicts with whatever lab scenario you are running. Once the configuration has been completed just issue a copy run flash:base.cfg (or whatever you want to name your base configuration file).

Step 2 > Creating the TCL script

My lab is completely isolated from outside network connectivity (only my console server is remotely accessible). This is great for ensuring that my mistakes learning opportunities don’t impact any network outside of my lab but can be a pain when trying to move files around. If you are in the same situation, you can use TCL to generate your TCL script. Yes, it does sounds like the inception plot played out on the CLI, but it does indeed work fine for this purpose. Here is the script I have used to generate my reset script (this would be executed from privileged exec mode):

Routers:
———-
tclsh
puts [open “flash:reset.tcl” w+] {
puts “Erasing Configuration”
typeahead “r”
puts [ exec “write erase” ]
puts “Copying Base Configuration To Startup”
typeahead “r”
puts [ exec “copy flash:base.cfg nvram:startup-config” ]
puts “Reloading The Router”
typeahead “r”
puts [ exec “reload” ]
}

Switches:
———-
tclsh
puts [open “flash:reset.tcl” w+] {
puts “Erasing Configuration”
typeahead “r”
puts [ exec “write erase” ]
puts “Erasing VLAN Database”
typeahead “r”
puts [ exec “del flash:vlan.dat” ]
puts “Copying Base Configuration To Startup”
typeahead “r”
puts [ exec “copy flash:base.cfg nvram:startup-config” ]
puts “Reloading The Router”
typeahead “r”
puts [ exec “reload” ]
}

Lets take a second to evaluate what is happening here

  • The tclsh command puts us into the tcl shell
  • The next line issues opens the file reset.tcl (you can call it whatever you want) and sets it to writeable. It is important to note the space between the ] and the {. Without that space you will have issues getting this to work.
  • From there on out we are just writing lines into the file we opened. This is the procedure for saving the running config (ensuring no unexpected save prompts during the process), erasing the current startup-config, erasing the VLAN database (on switches), replacing the startup-config with your base configuration file, and then reloading the device. Additionally we are writing output to the terminal throughout the process to indicate where we are in the script. Make sure that any commands in the script that require the curly brackets are fully opened and closed. An unexpected closing curly bracket (}) will cause the file to be closed for writing.
  • The last line is the closing bracket from the original open command. This will indicate you are done writing to the file.

From here you just need to issue the “tclquit” command to exit the tcl shell. You can verify that your file has been created by first checking for it’s existence (dir flash:) and then by reading the file to the terminal by using the “more flash:reset.tcl” command. That output should look like this:

R1#more flash:reset.tcl
puts [ exec “write” ]
puts “Erasing Configuration”
typeahead “r”
puts [ exec “write erase” ]
puts “Copying Base Configuration To Startup”
typeahead “r”
puts [ exec “copy flash:base.cfg nvram:startup-config” ]
puts “Reloading The Router”
typeahead “r”
puts [ exec “reload” ]
R1#

Step 3 > Issuing the reset command

Once the above steps have been competed, running the script is simple. Just type in “tclsh reset.tcl” from privileged exec mode and the script will be off and running. When completed you should have your device back to your known good configuration.

This can become even more convenient if you are using SecureCRT. Just open up the chat window, set it to send commands to all tabs, and then issue the “tclsh reset.tcl” command in the chat window. Your entire lab can be rest to your known good configuration in just a couple of minutes.

This simple automation has saved me a considerable amount of non-productive study time. I’m sure the process could be further automated but this has been sufficient for me up until this point. Maybe some day I will look into automating it further but hopefully you have found this information valuable in automating your own study environments.

3 comments

  1. Jordan,

    Nice write-up and explanation. Have you do anything else with TCL scripts for automating your CCIE study LAB’s?
    I have the same style script running in my LAB, but I’m still trying to figure out the best method to automate the load of lab configurations. I would love to be able to load for instance IPexpert Workbook 1, LAB 31 from my terminal-server with a TCL.

    I was thinking of loading all the initial configs into the compact flash cards on each router/switches and running the same style scripts as the eraseconfiguration.tcl , but i was wondering if you had seen or done anything else like this.

    1. Joe,

      I had considered doing something like that but just never got around to automating it. You should do it thought, I would love to see this expanded upon as an even better resource for those just getting started 🙂

      Jordan

  2. Hi Jordan,

    Thanks for sharing this info and a great write on how to implement. I’m working towards my CCNP presently and will be going on to CCIE R&S thereafter.

    I use GNS3 and also have built an extensive hardware lab and have been struggling with ways to automatically load configurations.

    Using TCL to either copy configurations from a flash storage or via tftp seemed to be the most practical thus my search on the topic and finding your write up.

    Good luck with the studies.

    Mike Shafer

Leave a Reply

Your email address will not be published. Required fields are marked *