Using TCL to Prepare Configuration

If you have worked as a network engineer for an enterprise or even a telco, you would notice that the best practice to have a standard configuration template. Sometimes, you are stuck in a situation wherein you need to prepare configuration let's say for around 20 routers and time is not on your side. My approach for this when I was starting my networking career was to get that standard template and start filing up the necessary configuration in notepad for the 20 routers and save one file after another. Believe me it was not an easy task and it was prone to having typo's.

It is for sure a tedious task but using TCL, it will pretty much make your life easier. I have researched for a way to automate the config preparation provided you have all the necessary data required. I am not a programmer but somehow I managed to find some TCL software and commands to make this possible. Before we begin we would need to have TCLKIT which can be downloaded here.

Now for this example, let us only try to create configs for 10 routers. Our standard config is as shown below. (not so long so make things easier)


hostname (hostname)
!
interface Serial1/1
ip address (ip address)(mask)
!
router ospf 1
network (network) (wildcard) area (ospf area)


The first step is to create our variables, quite much work required for this especially for long standard configs. We will create variables for those with () in the standard config above. These are the parts in the configuration wherein the data will be placed. Standard configuration with variables shown below.


hostname $hostname
!
interface Serial1/1
ip address $ipaddress $ipmask
!
router ospf 1
network $network $wildcard area $ospfarea

Now we have created our variables. Let us use the multivariable "foreach" TCL command to create a looping script. We put in our variables next to the "foreach" statement. The "$" is not required. If you are not familiar with this, please visit this post.


foreach {hostname ipaddress ipmask network wildcard ospfarea}

The next line of this script will now contain the data. Prepare the data in excel spreadsheet and the sequence of the columns should be the same as the one listed in the "foreach" statement. Then add that to the second line of the script. Put an open { before the data and } after the data.
Add also the important commands below that will make auto text file generation for each config file. The final script will look like something below. Then save this as a text file.


foreach {hostname ipaddress ipmask network wildcard ospfarea} {

Router1 1.1.1.1 255.255.255.0 1.1.1.1 0.0.0.0 1
Router2 1.1.1.2 255.255.255.0 1.1.1.2 0.0.0.0 2
Router3 1.1.1.3 255.255.255.0 1.1.1.3 0.0.0.0 3
Router4 1.1.1.4 255.255.255.0 1.1.1.4 0.0.0.0 4
Router5 1.1.1.5 255.255.255.0 1.1.1.5 0.0.0.0 5
Router6 1.1.1.6 255.255.255.0 1.1.1.6 0.0.0.0 6
Router7 1.1.1.7 255.255.255.0 1.1.1.7 0.0.0.0 7
Router8 1.1.1.8 255.255.255.0 1.1.1.8 0.0.0.0 8
Router9 1.1.1.9 255.255.255.0 1.1.1.9 0.0.0.0 9
Router10 1.1.1.10 255.255.255.0 1.1.1.10 0.0.0.0 10

} {set data "

hostname $hostname
!
interface Serial1/1
ip address $ipaddress $ipmask
!
router ospf 1
network $network $wildcard area $ospfarea

"
set filename "${hostname}.txt"
set fileId [open $filename "w"]
puts -nonewline $fileId $data
close $fileId

}

Now its time to auto generate the configs. What this looping script does is take the first line on the data, do the variable substitution and then at the end it will save the text file with the hostname as the filename. It does this until the last line of the data. The files will be auto generated where the TCLKIT software is saved.




Open TCLKIT, two windows will appear big and small. Click on File -> Source ->Go to the directory where you saved the script as text file -> Change "Files of Type to "All Files" -> Select the Script. Then viola, your configurations appear and all variables substituted. It makes life easier for a network engineer.





Certifications

Certifications

The Dreamer

A fun loving person who enjoys learning new things. Currently working as a Network Engineer supporting the global network of a Fortune 500 company. This blog serves as my notes for the labs I created for my CCIE journey. I can guarantee there are errors in my posts. If you spot them, please let me know.

Join my Facebook Page I WANT TO BE A CCIE

Donate to the Cause

My aim is to create materials for free and possibly a free lab. If you wish to help out, please send any amount. Thanks.

Join my Bandwagon

Blogs that I Read