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.

10 comments:

  1. What if you wanted to query for a range of subinterfaces to shutdown?
  2. Your version of IOS might support interface ranges in configuration mode, so you could use that.

    Or you could change the script. Like any other language, Tcl has loop constructs.
  3. Great script...used it on my router and working perfectly..

    However.. I wanted to do the same thing with an interface of accesspoint..too bad.. This one is not supporting tcl scripts.. :(

    So what i was thinking... Is this script easy to edit that it can telnet to the accesspoint and push some cli commands.. Enable,conf t interface dot11radio0,shutdown ???

    Thankssss :)
  4. Great scripts... I was wondering.. Is it also possible to execute a script like this with a callmanager express by the phone? So for example you dial a number that activates this script... Will be great...then i can execute scripts without a pc :-D
  5. Sure you can do that - IVR supports Tcl scripts.
  6. Ok,so i think i can just create another dial-peer that activates a service of my onw right?
    Like service flash:script.tcl ?? That will be great..

    I just want to activated some ios commands on this router when i'm calling the dialpeer number :)
  7. Ok,one step further.. i can now execute the command on my router... however.. when i tried to put this to a dialpeer, i get the message could not load IVR script..

    So how can i use a tcl script with the ivr applications?

    Thanks
  8. Time to find someone that knows something about IVR scripts ;)
  9. Ok,i had hope it was you ;)

    I will search further.. Thanks anyway for sharing you're knowledge.. :)
  10. Hi,

    What will the script look like if I had to run the following commands:

    no aaa accounting network default start-stop group tacacs+
    no aaa accounting resource default stop-failure group tacacs+

    This is just an example....
Add comment
Sidebar