Saving and running TCL scripts from flash

Anyone who is studying for the CCIE knows a lot about TCL scripting. Most certainly a big timesaver.

Of course TCL scripts can also be used in the real world. You don’t want to have to type our your script all the time, so is there a better way?

Over at networking-forum.com I asked if someone could come up with a script that would check for a route in any given subnet and return a ‘No route found’ for any IP address not in my global route table. I forgot who came up with it for me (sorry) but here is an example. Let’s say I wanted to check what routes I had for the range 10.20.16.0/20 – Of course usually this would be for a public range, but work with me here. This is the script:

#set the starting IP address octets
set firstoctet 10
set secondoctet 20
set thirdoctet 16
set fourthoctet 0

#while we're less than 10.20.31.255
while { $thirdoctet <= 31 } {
      while { $fourthoctet <= 255 } {
            set ipaddr $firstoctet.$secondoctet.$thirdoctet.$fourthoctet
            set results [exec "show ip route $ipaddr"]
            
            if { [regexp "not in table" $results] } {
                  puts "No route found for $ipaddr"
            }
            
            incr fourthoctet
      }
      
      #if out of the inner loop, reset the fourth back to 0 and increment the second octet
      set fourthoctet 0
      incr thirdoctet
}

This script works wonders, but it's pretty big to have to even copy and paste in each time I want to run it.

What I've now done is save the above script into a simple text file and renamed it to 10_20_16_0.tcl - I then tftp'd is to the flash on my router

There are now a couple of ways to run this file. The first way is to manually run it when I need to. How? Like so:

Router#tclsh flash:/10.20.16.0.tcl
No route found for 10.20.16.40
No route found for 10.20.16.41

The source will simply load and execute the tcl script straight away. This is certainly handy for those scripts you only want to run now and then.

What if you want to run a script automatically? Why not use EEM :)

event manager applet TCL_RUN
 event none
 action 1.0 cli command "enable"
 action 1.1 cli command "tclsh flash:/10_20_16_0.tcl"

The beauty of using EEM is that it can track all kinds of things, and based on those events it can run tcl scripts for you.

To actually run the EEM script yourself you can do so like this:

event manager run TCL_RUN

Nice and easy :)