Showing posts with label TCL. Show all posts
Showing posts with label TCL. Show all posts

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.





Fun with TCL: Generating 100 Loopbacks in 1 Minute

When I did the BGP Maximum-Prefix post, I used Excel to generate 100 ip addresses, just by typing 1.1.1.1 then dragging all the way down to 100. On the cell to the left, I put "network" then to the right "mask 255.255.255.255". I pasted, it under BGP and I noticed there are some errors because of the line breaking. I hate to paste over and over again. I'd rather be effective than persistent.:)

So after an hour of research and experimentation, I found a way to generate 100 ip route commands without any problem. I tried it on loopback interfaces configuration and it worked fine! Check the script below.


foreach number {
1
2
3
4
5
6
} { puts [ ios_config "interface Loopback$number" ] }

It's almost the same as the common TCL ping script I use but the keyword "ios_config" made the difference. This keyword makes you execute any global configuration command in TCL. For example's sake I used only 6 numbers. I will post later the 100 loopbacks I created.

Now what the heck is a loopback without any ip address. Useless isn't it? I also found a way to map an ip address to a loopback in TCL. You use multiple variables.


foreach {number address} {
1 3.3.4.1
2 3.3.4.2
3 3.3.4.3
4 3.3.4.4
5 3.3.4.5
6 3.3.4.6

} { puts [ ios_config "interface Loopback$number" "ip address $address 255.255.255.255" ] }

Now, if you notice after the "interface Loopack$number" there is a subcommand for interface configuration mode. You can add as many commands as you want like descriptions. Just enclose it with parenthesis.

Just be creative with your script. I also used it to announce 500 prefixes in BGP just for fun. As promised here is my "show ip interface brief" showing the loopbacks.


Router#sh ip int br
Interface IP-Address OK? Method Status Protocol
FastEthernet0/0 unassigned YES unset administratively down down
Loopback1 3.3.4.1 YES unset up up
Loopback2 3.3.4.2 YES unset up up
Loopback3 3.3.4.3 YES unset up up
Loopback4 3.3.4.4 YES unset up up
Loopback5 3.3.4.5 YES unset up up
Loopback6 3.3.4.6 YES unset up up
Loopback7 3.3.4.7 YES unset up up
Loopback8 3.3.4.8 YES unset up up
Loopback9 3.3.4.9 YES unset up up
Loopback10 3.3.4.10 YES unset up up
Loopback11 3.3.4.11 YES unset up up
Loopback12 3.3.4.12 YES unset up up
Loopback13 3.3.4.13 YES unset up up
Loopback14 3.3.4.14 YES unset up up
Loopback15 3.3.4.15 YES unset up up
Loopback16 3.3.4.16 YES unset up up
Loopback17 3.3.4.17 YES unset up up
Loopback18 3.3.4.18 YES unset up up
Loopback19 3.3.4.19 YES unset up up
Loopback20 3.3.4.20 YES unset up up
Loopback21 3.3.4.21 YES unset up up
Loopback22 3.3.4.22 YES unset up up
Loopback23 3.3.4.23 YES unset up up
Loopback24 3.3.4.24 YES unset up up
Loopback25 3.3.4.25 YES unset up up
Loopback26 3.3.4.26 YES unset up up
Loopback27 3.3.4.27 YES unset up up
Loopback28 3.3.4.28 YES unset up up
Loopback29 3.3.4.29 YES unset up up
Loopback30 3.3.4.30 YES unset up up
Loopback31 3.3.4.31 YES unset up up
Loopback32 3.3.4.32 YES unset up up
Loopback33 3.3.4.33 YES unset up up
Loopback34 3.3.4.34 YES unset up up
Loopback35 3.3.4.35 YES unset up up
Loopback36 3.3.4.36 YES unset up up
Loopback37 3.3.4.37 YES unset up up
Loopback38 3.3.4.38 YES unset up up
Loopback39 3.3.4.39 YES unset up up
Loopback40 3.3.4.40 YES unset up up
Loopback41 3.3.4.41 YES unset up up
Loopback42 3.3.4.42 YES unset up up
Loopback43 3.3.4.43 YES unset up up
Loopback44 3.3.4.44 YES unset up up
Loopback45 3.3.4.45 YES unset up up
Loopback46 3.3.4.46 YES unset up up
Loopback47 3.3.4.47 YES unset up up
Loopback48 3.3.4.48 YES unset up up
Loopback49 3.3.4.49 YES unset up up
Loopback50 3.3.4.50 YES unset up up
Loopback51 3.3.4.51 YES unset up up
Loopback52 3.3.4.52 YES unset up up
Loopback53 3.3.4.53 YES unset up up
Loopback54 3.3.4.54 YES unset up up
Loopback55 3.3.4.55 YES unset up up
Loopback56 3.3.4.56 YES unset up up
Loopback57 3.3.4.57 YES unset up up
Loopback58 3.3.4.58 YES unset up up
Loopback59 3.3.4.59 YES unset up up
Loopback60 3.3.4.60 YES unset up up
Loopback61 3.3.4.61 YES unset up up
Loopback62 3.3.4.62 YES unset up up
Loopback63 3.3.4.63 YES unset up up
Loopback64 3.3.4.64 YES unset up up
Loopback65 3.3.4.65 YES unset up up
Loopback66 3.3.4.66 YES unset up up
Loopback67 3.3.4.67 YES unset up up
Loopback68 3.3.4.68 YES unset up up
Loopback69 3.3.4.69 YES unset up up
Loopback70 3.3.4.70 YES unset up up
Loopback71 3.3.4.71 YES unset up up
Loopback72 3.3.4.72 YES unset up up
Loopback73 3.3.4.73 YES unset up up
Loopback74 3.3.4.74 YES unset up up
Loopback75 3.3.4.75 YES unset up up
Loopback76 3.3.4.76 YES unset up up
Loopback77 3.3.4.77 YES unset up up
Loopback78 3.3.4.78 YES unset up up
Loopback79 3.3.4.79 YES unset up up
Loopback80 3.3.4.80 YES unset up up
Loopback81 3.3.4.81 YES unset up up
Loopback82 3.3.4.82 YES unset up up
Loopback83 3.3.4.83 YES unset up up
Loopback84 3.3.4.84 YES unset up up
Loopback85 3.3.4.85 YES unset up up
Loopback86 3.3.4.86 YES unset up up
Loopback87 3.3.4.87 YES unset up up
Loopback88 3.3.4.88 YES unset up up
Loopback89 3.3.4.89 YES unset up up
Loopback90 3.3.4.90 YES unset up up
Loopback91 3.3.4.91 YES unset up up
Loopback92 3.3.4.92 YES unset up up
Loopback93 3.3.4.93 YES unset up up
Loopback94 3.3.4.94 YES unset up up
Loopback95 3.3.4.95 YES unset up up
Loopback96 3.3.4.96 YES unset up up
Loopback97 3.3.4.97 YES unset up up
Loopback98 3.3.4.98 YES unset up up
Loopback99 3.3.4.99 YES unset up up
Loopback100 3.3.4.100 YES unset up up

I wonder how many loopbacks I can create in a Cisco router. Maybe I'll try that some other time. Good day mates! :)

Simple TCL Ping Script

Doing ping tests for lots of IP addresses can be tiring since you can't paste all the ping commands at the same time. You have to do it one at a time. All you need is Patience or you can opt for a TCL scripting language which is already available in Cisco IOS 12.2(25). At first, I thought you need to learn TCL scripting to be a CCIE but nah! :) All you need is the basics and if you are interested learning more about TCL you can click here. To access the tcl command line in Cisco router issue the "tclsh" command. For the ping script just modify the ip addresses what is shown below and it should be good.


foreach address {
1.1.1.1
2.2.2.2
3.3.3.3
4.4.4.4
5.5.5.5
6.6.6.6
} { puts [ exec "ping $address" ] }

The word "address" here is just a variable, you can substitute this with anything you want. Now lets try applying this to the Cisco router.


R0#tclsh
R0(tcl)#foreach address {
+>(tcl)#1.1.1.1
+>(tcl)#2.2.2.2
+>(tcl)#3.3.3.3
+>(tcl)#4.4.4.4
+>(tcl)#5.5.5.5
+>(tcl)#6.6.6.6
+>(tcl)#} { puts [ exec "ping $address" ] }

Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 1.1.1.1, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 4/44/100 ms

Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 2.2.2.2, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 8/27/56 ms

Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 3.3.3.3, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 1/26/96 ms

Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 4.4.4.4, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 4/27/52 ms

Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 5.5.5.5, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 4/27/96 ms

Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 6.6.6.6, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 4/32/84 ms

How cool is that!!! This script can be useful when you are doing your CCIE lab exams as they say, when you want to verify that every subnet in your lab routers are reachable. You can try this in your own Dynamips lab. This should be safe to be done in the production routers but where I work, I doubt if this will be allowed by the company's IT policy. :P

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