Category: Tcl
You fix some, you break some ...
Example: Tcl script with command-line parameters
In a comment to the “Execute multiple commands at once” post, Michal has asked for a complete Tcl-shell-with-parameter example. Here's a short script that shuts down the interface and displays its status:
- Variable ifname is set to the value of the first command-line parameter (in many other programming languages, this would be written as argv[0]);
- If the ifname is empty, the script aborts and prints the usage guidelines (again, in a more human-oriented programming language, this would be if (ifname == “”) ...);
- The show ip interface ifname command is executed. If it fails, the interface name is not correct and the script aborts.
- IOS configuration commands interface ifname and shutdown are executed.
- The show ip interface brief configuration command is executed and filtered with the interface name.
#
# ifname is set to first CLI parameter (interface name)
#
set ifname [lindex $argv 0]
if {[string equal $ifname ""]} { puts "Usage: shutdown ifname"; return; }
if { [ catch { exec "show ip interface $ifname" } errmsg ] } {
puts "Invalid interface $ifname, show ip interface failed"; return}
ios_config "interface $ifname" "shutdown"
puts [ exec "show ip interface brief ¦ include $ifname" ]
If you store this Tcl script into your flash as shutdown.tcl and configure alias exec shutdown tclsh flash:shutdown.tcl, you can execute the command shutdown Serial0 to shut down the serial interface.
Notes:
- The last show command will display the interface status only if the specified interface name exactly matches the actual IOS interface name (whereas the rest of the script accepts shortcut names). The more generic matching algorithm is left as an exercise for the reader
- For more in-depth information on Tclsh implementation on Cisco IOS, read the IOS Tclsh resources.
- This article is part of You've asked for it series.
Update: The “show ip interface” command I've always wanted to have
After I've published the Tcl script that displays the interface IP parameters in a formatted table, cos quickly pointed out a bug: I've expected the IP addresses in the address mask format. In the meantime, I've figured out the root cause of the problem (our remote labs are set to display IP masks in decimal format for compatibility reasons) and fixed the Tcl script. It temporarily sets the terminal ip netmask-format to bit-count before executing the show command. The new script recognizes three parameters:
- active: display only interfaces that are up/up;
- configured: display only interfaces with configured IP addresses (unnumbered interfaces using IP address of an interface without one count as configured since IOS reports their IP address as 0.0.0.0).
- address: displays IP address of the unnumbered interface, not the interface that it's borrowing the address from.
The “show ip interface” command I've always wanted to have
To use it, download it and store it into the flash memory of your router. Configure alias exec ipconfig tclsh flash:ipInterfaces.tcl and you can use ipconfig or ipconfig active to display interface IP addresses.
Reload EEM Tcl policy with help of Tcl shell
- Source file is usually edited on a general-purpose workstation.
- The file has to be downloaded to router's local storage (EEM does not register non-local policies).
- The new version of the EEM policy has to be registered with EEM with event manager policy configuration command
- After all these steps, the new policy can be tested.
set policy [lindex $argv 0]To use the script, follow these steps:
set source "tftp://10.0.0.10/tcl/" # replace with your host and directory
set destination "nvram:" # replace with local storage device
if {[string equal $policy ""]} {
return -code error "expected policy name"
}
puts "replacing policy: $policy"
ios_config "file prompt quiet"
ios_config "no event manager policy $policy" ""
exec "copy $source$policy $destination$policy"
ios_config "event manager policy $policy"
ios_config "no file prompt quiet"
- Save the script in a .tcl file (for example, changePolicy.tcl)
- Change the script parameters (remote host and local storage)
- Save the .tcl file to your router's local storage (you can also run it from a remote server)
- Configure a command alias, for example alias exec eem tclsh flash:changePolicy.tcl testPolicy.tcl
Alternatively, if you define alias exec eem tclsh flash:changePolicy.tcl, you can specify policy name as an argument to the eem command, for example eem testPolicy.tcl.
Command Authorization Fails with EEM applet or Tcl policy
One of my readers asked an interesting question: „why do the commands executed within a EEM Tcl policy fail with Command authorization fails message?“ The short answer is simple: If you use AAA command authorization (which you can only do if you're using a TACACS+ server), you have to specify the username under which the EEM will execute its CLI commands with the event manager session cli username user configuration command.
Use Tcl script to change the interface status
- Enter configuration mode;
- Select the appropriate interface with the interface loopback x command;
- Try to remember whether you need to disable or enable it;
- Issue the shutdown or no shutdown command;
- Exit the configuration mode and continue your debugging/testing process.
After a particularly boring testing session I decided to write a Tcl script to automate the job. To use it, download it and store it into ifchange.tcl. Download the Tcl file to your router (Flash or NVRAM) and define an alias: alias exec ifchange tclsh flash:ifchange.tcl. Now you can use the new ifchange command to change interface status.
IOS Tclsh resources
- Running Tcl procedures from IOS command line
- Tclsh command line parameters
- Where does Tcl output go
- Executing IOS commands from Tcl shell
- IOS scripting with Tcl (IOS 12.3T documentation)
- TCL'ing your Cisco router
And last but not least, if you want to store Tcl procedures on your router and don't want to write into the router's Flash memory (I hate that the router prompts me whether I want to erase the flash every time I store something into it), you can store them in NVRAM.
Tclsh Command Line Parameters
In a previous post, I’ve described how to execute a Tcl file with the tclsh command.
You can do even more than that: you can pass parameters to the executed file. Every word you enter after the file name in the tclsh command line is passed as a parameter to the Tcl code you execute. To get these parameters in Tcl, use Tcl commands similar to the code below:
Executing IOS Commands from Tcl Shell
The Tcl procedures used to execute IOS commands in Embedded Event Manager (cli_open, cli_write …) don’t work when you start Tcl shell from command line interface. To execute IOS commands in this context, use:
- exec command to execute an exec-level command, for example exec “show ip route”
- ios_config mode command to configure the router
If the first parameter of the ios_config command is a global configuration command, you shall omit the second parameter (for example, ios_config “hostname router”). To configure a parameter in one of the sub-configuration modes (for example, interface state), use the first parameter to specify the configuration mode and the second parameter as the actual configuration command (for example, ios_config “interface loop 0” “no shutdown”).
Where does the Tcl output go?
There are two scenarios where Tcl would execute in context of a line: if you start a Tcl procedure with the tclsh command or if it's an EEM policy registered with the event_register_cli with sync parameter set to yes.
Running Tcl Procedures from Cisco IOS CLI
Starting in IOS release 12.3(2)T, Tcl shell is accessible from the command line interface with the tclsh command. After entering this command, you get the Router(tcl)# prompt and can enter individual Tcl commands (the help is confusing, though – you get help on exec-mode commands, but none of them work).