TCL PING Script
After “The Ghost” and I worked juntos in Corporate America for quite a few years, we came up with a few scripts to help automate some mundane tasks. The following TCL script can help you test quickly if there is reachability via PING (as long as it is not being filtered in transit of course) for multiple endpoints/routers/hosts/etc. This script in particular is actually a bit of an upgrade/tweak to the scripts we both used on our CCIE Lab attempts. It really helps when you have a bunch of IPs that you want to verify (contiguous or scattered around). Let’s setup an example to demonstrate it.
Let’s say you have 7 IP Addresses in the 192.168.7.0/24 subnet (numbers 1-7) that you want to ping quickly for verification after a config change (or after a BX style upgrade). Well, your first option is to ping them directly from one router, one by one, and wait… Change the ip after the results, ping again, and wait… Etc, etc, etc. Or, you can leverage TCL (Tool Command Language), we can quasi-automate this process on our behalf with a little bit of modification to this template I am releasing here in the post.
General disclaimer... This is a script that has been proven useful for both "The Ghost" and I both in a lab environment and in production but your results may vary. Always, always, ALWAYS test this in a controlled environment before putting it into production. Just to illustrate the fact, I'll call on my girl EVE(-NG) and test it with her. I'm going to just throw in 8 routers and a single layer 2 switch. Below is our simple overall topology, just so we are all on the same page...
All that I have configured in this lab so far are: The device hostnames, VTP v2 config (out of habit), VLAN7 on the L2 Switch (where the router G0/0 interfaces are all sitting) and the G0/0 config on each router as 192.168.7.x/24, where X is the router number. (Don't worry... I'm releasing the topology with this post, so you can test yourselves.)
So, now that we are all on the same page with the setup for this lab, let’s see why I’m even writing this post for everyone and start “automating” pings already! TCL is a full blown programming language that is embedded in almost every (if not every) Cisco IOS and IOS-XE device (I haven’t used IOS-XR nor NX-OS for this process yet, so I can’t speak to them). I’m 99% certain that TCL can help “automate” more than just pings, but let’s do this first and then maybe you can write your own TCL Script(s) to help you in your day to day life. Feel free to use this script as a springboard.
Let’s get started. What is the purpose of this? To help us automate the ping command to confirm connectivity between hosts. First, we first have to choose a device to run the script from; I’m going to use the Layer2 Switch in this example. To start the process of invoking TCL for us, the first command we need to enter the the “tclsh” command from Privileged exec (enable) mode, to invoke or activate “TCL mode” as I refer to it.
As you can see, the prompt changes to include (tcl) after the hostname to signify that we have changed into “TCL mode.” So now that we are in this mode, we can create a process to run. I typically build this process within a text editor so I can modify it as needed, and then, I paste it back into the device I am running it from. But you can also make it as a TCL file, and upload it to flash. 6 of a dozen, half of another, I think that saying is… Anyway…
Notepad++ is my weapon of choice if I’m on a PC doing this work or TextEdit is a nice simple program while on a Mac (where I am today). We’ll name this process “TheTickla” (feel free to use whatever name you want); just remember the name as we will need it later on. That last sentence will make more sense once we take a look at the template I have setup.
So here is a 1000ft view of what this script is doing: We are going to build a process called “TheTickla” and it is going to load up IP Addresses 192.168.7.1 through 192.168.7.8 as variables. Then we are going to ping each of these addresses 4 times with a timeout of 1 second (per ping attempt). Based on the output of the pings, we’re going to send to the CLI either a smiley face or a sad face with some text signifying either a Pass or Fail.
If you need to modify anything here, feel free to mess with it in order to suit your individual use case for testing (IN THE LAB FIRST PLEASE).
So now that we have the script setup, let’s run it in the topology and see our outputs. Bear in mind, we are already in “TCL Mode” on the L2 Switch. So we can just enter the script and then invoke it.
Note that you should be returned to a (tcl) prompt and that means the TCL Script seems to be formatted properly, and we have all of our required braces ({ and }), so we can now call the process. In the previous screenshot, you can see I called it by its name TheTickla.
OK, see how we did… It looks like everyone except Router6 is working properly. Let’s take a peek at Router6’s config and see if we can’t isolate, identify and rectify the issue.
Well that’ll do it. The wrong IP address will not respond to what we told the Script to ping. So let’s fix it and re-run the original script. The nice thing is, it will still be in memory on the Switch until you quit TCL so we just have to call it again by its name.
Success for all 8 IPs!!! We are ALMOST done with the script. Just don’t forget to quit out of “TCL mode” before you do any other configs on the device that you have built the script on. To do this, use the “tclquit” command and you will be dropped back onto the privileged exec prompt that we all know and love.
It’s good practice to quit out of TCL mode in general. Personally, I’ve had issues in the past with random Variables that were being called inadvertently while doing other administration of devices. Bottom line, in my opinion, quit out of TCL mode (tclquit) and then drop your SSH connection. Then hop back on and make your regular changes. This will keep you working on one task at a time.
Well, I hope this article helps someone or some people out who are looking for help to automate pings. Feel free to use this script as a template to build your own TCL scripts. Also, feel free to change the outputs for passing and failing pings. This script is for a one time use, you can make this a TCL file and upload it to Flash if you want to run it multiple times; however, you will have to keep uploading it after you make changes via SCP/FTP/TFTP/etc since I’m not aware of how to modify the TCL Scripts once they are on flash. So, in this use case, I typically copy and paste the script each time I use it into my SSH or Console session; again your mileage may vary
You’ve been patient enough, so here is the TCL Script in text form:
proc TheTickla {} {
foreach ADDRESS {
192.168.7.1
192.168.7.2
192.168.7.3
192.168.7.4
192.168.7.5
192.168.7.6
192.168.7.7
} {set RESULT [exec “ping $ADDRESS repeat 4 timeout 1”];
if { [regexp “!!!” $RESULT ]} {
puts “$ADDRESS –> >: – ) PASS”
} else {
puts “$ADDRESS –> : – ( FAIL”
}
}
}
The EVE-NG Topology is available for download here.
Until the next one,
– 51406 aka Tony, “The Mechanic…” –