Wednesday, 29 August 2012

Creating multiple Loopback addresses using a Tcl script

In a previous blog, (Some clever PING tricks) I discussed using Tcl to create PING scripts to ping multiple hosts. This got me thinking, what other cool applications can I demonstrate using Tcl.

The first thing to spring to mind was creating multiple Loopback addresses. The aim of this blog is to discuss how to create multiply Loopback addresses on a router using a Tcl script.

"Why would you want to create loads of loopback addresses?" I hear you ask...
When testing routing protocols in a lab environment (Especially BGP) it is good to have plenty of routes in your routing table. Rather than create loads of routers in GNS3, or fill a rack with kit you don't need, it is easier to simulate other networks using Loopback address on a Router and then add them to our routing protocol. That's not the objective of this blog, this blog is to discuss how to create them.

As with the PING script, I will start by saying you could write all your loopbacks in a word document, and then cut them into your router, but doing it this way, only takes one line of code. (although, I may split it up to make it easier to understand).

The last PING script used the for command to create what in programming terms is called a Control Flow Loop.

The correct syntax is;

for init test body

Or if we display this in a Tcl command it would look like this;

for { init } { test } { next } {
body
}

Look familiar?

for { set i 1 } {$i <= 9 } {incr i } {
ping 10.0.0.$i re 10 si 1500 so lo0 df-bit
}

This was our last PING script in the last blog.
By changing our variable, and adapting the body we can use this loop to create our loopback addresses.

for { set i 0 } {$i <= 255 } { incr i } {
puts [ ios_config "interface loopback$i" "ip address 10.255.$i.255 255.255.255.255" ]
}

So what I've done now is "set" "i" (our variable) at 0.
The conditional test is to make sure "i" is less than 255. We can't have an octet in a IP address greater than 255.
We then increment "i" by one each time the loop is run.
Finally we use the "puts" command to send data to the router.
As we are now using configuration commands on our router so we need to use "ios_config" rather than  the "exec" we used in our other PING script.

The commands should be self-explanatory;
interface loopback$i Is the IOS command to create a loopback interface. We use "$i" our variable to change the loopback each time the script loops.

ip address 10.255.$i.255 255.255.255.255 Is to create the IP address. I have chosen to put our variable in the third octet, but in theory we could place it in different octets, or even in several octets.
(I.E. 10.255.$i.$i)

So lets test our script.


We can test our script by looking at our interfaces with the "show ip interface brief" command;

No point showing all 255 loopbacks, but just to prove I didn't cheat, here is the end of that show ip interface brief command;


Voila!
Our script worked and we have just created 255 loopbacks with a single line of Tcl scripting.

I hope this blog has been of interest, and helped you understand a little more about the power of scripting!

1 comment: