IOS Scripting with Tcl

Discovered during Cisco CCNP course, Tcl (originally from “Tool Command Language“) is a scripting language created by John Ousterhout and introduced from IOS version 12.3(2)T.

But what is Cisco Tcl?
The Cisco IOS Tcl shell was designed to allow customers to run Tcl commands directly from the Cisco IOS CLI prompt. Cisco IOS software does contain some subsystems such as Embedded Syslog Manager (ESM) and Interactive Voice Response (IVR) that use Tcl interpreters as part of their implementation. These subsystems have their own proprietary commands and keyword options that are not available in the Tcl shell.

Several methods have been developed for creating and running Tcl scripts within Cisco IOS software. A Tcl shell can be enabled, and Tcl commands can be entered line by line. After Tcl commands are entered, they are sent to a Tcl interpreter. If the commands are recognized as valid Tcl commands, the commands are executed and the results are sent to the tty. If a command is not a recognized Tcl command, it is sent to the Cisco IOS CLI parser. If the command is not a Tcl or Cisco IOS command, two error messages are displayed. A predefined Tcl script can be created outside of Cisco IOS software, transferred to flash or disk memory, and run within Cisco IOS software. It is also possible to create a Tcl script and precompile the code before running it under Cisco IOS software.

Multiple users on the same router can be in Tcl configuration mode at the same time without interference because each Tcl shell session launches a separate interpreter and Tcl server process. The tty interface number served by each Tcl process is represented in the server process name and can be displayed using the show process CLI command.

The Tcl shell can be used to run Cisco IOS CLI EXEC commands within a Tcl script. Using the Tcl shell to run CLI commands allows customers to build menus to guide novice users through tasks, to automate repetitive tasks, and to create custom output for show commands.

To enter in the “Tool Command Language shell” type “tclsh” command, while to exit type “tclquit“.

Remember: errors in Tcl scripts can cause infinite loops in the router.

1. A very simple example: Hello world
Copy this function in the Tcl configuration mode

proc test {} {
puts "Hello world!"
}

The result will be:

Ciscozine#tclsh
Ciscozine(tcl)#proc test {} {

+>puts "Hello world!"
+>}

To test the script type “test”:

Ciscozine(tcl)#test
Hello world!

Ciscozine(tcl)#

2. Ping multiple IP addresses
Often during troubleshooting, it is needed to ping some IP addresses to test connectivity; in these situations Tcl could be very useful.

For instance, suppose to ping the first ‘x’ ip address of the 172.16.1.x/24 network. Usually a network administrator must type “ping 172.16.1.1” then “ping 172.16.1.2” and so on…, but it could take a long time.

Tcl could help us with a very simple script:

proc ping_net {x} {
 for {set n 1} {$n<=$x} {incr n 1} {
    exec "ping 172.16.1.$n"
 }
}

The result will be:

Ciscozine(tcl)#proc ping_net {x} {
+> for {set n 1} {$n<=$x} {incr n 1} {
+>    exec "ping 172.16.1.$n"
+> }
+>}

To test the first five IP addresses:

Ciscozine(tcl)#ping_net 5

Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 172.16.1.1, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 1/2/4 ms
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 172.16.1.2, timeout is 2 seconds:
.....
Success rate is 0 percent (0/5)
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 172.16.1.3, timeout is 2 seconds:
.....
Success rate is 0 percent (0/5)
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 172.16.1.4, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 1/2/4 ms
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 172.16.1.5, timeout is 2 seconds:
.....
Success rate is 0 percent (0/5)
Ciscozine(tcl)#

In these 2 simple examples, I have used 3 Tcl command: proc, puts, for.

The proc command creates a new command. The syntax for the proc command is: proc name args body
When proc is evaluated, it creates a new command with name name that takes arguments args. When the procedure name is called, it then runs the code contained in body.

The puts command is used to print “somethings”.

The for command in Tcl takes four arguments; an initialization, a test, an increment, and the body of code to evaluate on each pass through the loop. The syntax for the for command is: for start test next body
During evaluation of the for command, the start code is evaluated once, before any other arguments are evaluated.

You can find more informations about tcl command syntax on http://www.tcl.tk/.

References:

2 COMMENTS

  1. Hey, is there anyway you could modify the ping script? So that it pings the same IP address, just from a different source IP address every time. My situation: I test Cisco equipment at my Job and we’re trying to implement port testing of Switches. I have my set up but I just can’t figure out the script.

    I’m using a 3560 layer 3 switch. I have every port configured with a different IP address. So It goes 10.0.0.1 for port 1. 10.0.1.1 for port 2 etc. The 3rd octet is the only number that changes all the way through 48 ports. The switches I’m going to be testing are just gonna be an easy set up. Vlan 1 with IP of 10.0.0.2 /24. I also had to set up RIPv2 so I could have routes from every port to 10.0.0.2. Everything works I just need to automate this process and your script is the best I’ve found so far.

    So any help would be greatly appreciated.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.