TCL script for manipulating interfaces

Unanswered Question
Dec 8th, 2009


Not really sure who to turn to, so I thought this was as good place to start. I'm trying to write a script that would allow me to shutdown, no shutdown, or flap an interface. In other words, I would be able to shutdown the interface, no shut the interface, or flap (bring it either up or down depending on it's starting state for a specific amount of time). I found a pretty good script online, however there a few things that I would also like it to do.

proc usage {} { puts "Syntax: tclsh ifchange.tcl interface \[on|off|change|flap\]";}

proc doConfig { mode cmd } {
  if { [ catch { ios_config $mode $cmd } errmsg ] } { error "IOS configuration $mode / $cmd failed"; }

proc getState { ifnum } {
  if { [ catch { set ifstate [exec "show interface $ifnum"] } iferror ] } {
    error "No such interface: $ifnum";
  set result [expr [ string first {administratively down} $ifstate  ] < 0]
  return $result ;

proc changeState { ifnum } {
  puts "changing state of $ifnum" ;
  if { [getState $ifnum] } {
    puts "shut down interface $ifnum" ;
    doConfig "interface $ifnum" "shutdown"; } else {
    puts "enable interface $ifnum" ;
    doConfig "interface $ifnum" "no shutdown"; }

proc flapState { ifnum } {
  changeState $ifnum;
  puts "... waiting ...";
  after 6000;
  changeState $ifnum;

proc printState { ifnum } {
  if { [getState $ifnum] } { puts "interface is up"; } else { puts "interface is down" }

set ifnum [lindex $argv 0]
set ifstate [lindex $argv 1]
if {[string equal $ifnum ""]} { usage; return; }
if {[string equal $ifstate ""]} {
  if { [ catch { printState $ifnum; } errmsg ] } { puts stderr $errmsg; }

if { [ catch {
  switch $ifstate {
    on      { doConfig "interface $ifnum" "no shutdown";
              puts "Interface $ifnum changed state to on"; }
    off     { doConfig "interface $ifnum" "shutdown"
              puts "Interface $ifnum changed state to off"; }
    flap    { flapState $ifnum }
    change  { changeState $ifnum }
    default { usage; }
} errmsg ] } { puts stderr $errmsg; }

As it stands now, the script will do everything I need it to do except:

  • Instead of a static time of 6 seconds for the flap option, giving the user the option to pass an argument of how long they want the flap to last
  • Find a way to allow someone in User EXEC mode (read:priv level 1) to be able to run some sort of alias to process the script

I do not have any experience in TCL and very little in EEM. Any and all assistance would be greatly appreciated. Thanks again.

I have this problem too.
0 votes
  • 1
  • 2
  • 3
  • 4
  • 5
Overall Rating: 5 (1 ratings)
Joe Clarke Tue, 12/08/2009 - 13:20

First, this script has nothing to do with EEM.  It is a pure tclsh script.  Second, to create an alias, configure:

alias exec ifchange tclsh flash:/ifchange.tcl

Then you can run the script just by typing the "ifchange" command (and pass the appropriate arguments).

The attached script should do what you want in terms of flapping.  It will accept a new, optional argument when the flap command is specified.  That argument will be the number of seconds to wait before bringing the interface back up.  For example to flap the interface after 10 seconds:

ifchange Fa0/0 flap 10
kiddsupreme Tue, 12/08/2009 - 13:31


Thanks for your prompt response. This will definitely do the trick. The reason I brought up EEM, was because of the fact that a person in User EXEC mode (non-enable) cannot utilize tclsh. I was under the impression that utilizing "event manager run" aliased to a keyword and then set to a lower priv level would allow a work around. Am I mistaken? Is there no way to work around this issue? Thanks again.

Joe Clarke Tue, 12/08/2009 - 13:37

Ah, I follow.  No, tclsh requires privilege 15 to run.  EEM is a workaround, but the script will have to be rewritten.  On what version of IOS is this going to be run?

Joe Clarke Wed, 02/03/2016 - 09:52

The EEM script should work with any interface.  I don't see any changes that would be necessary for switch/mod/slot configs.

kiddsupreme Sun, 12/13/2009 - 06:29

Thanks again jclarke. The script does indeed work in EEM. Looks like this will do just nicely. Definitely wouldn't have figured that one out.

romainfauvet Thu, 02/13/2014 - 02:23

Hello , i want to know if this script is still valid ?

Cause , I want to write a script that would turn off some interface at an exact time.

Thank you

Joe Clarke Thu, 02/13/2014 - 10:05

This script is still valid in that it works with EEM on IOS devices.  You could use this as a foundation to do what you want.

romainfauvet Fri, 02/14/2014 - 01:10

And how is that possible to put the time from the clock in a variable ?

romainfauvet Fri, 02/14/2014 - 02:14

ok my bad.

I fund this

set time [clock format [clock seconds] -format "%H%M%S"]

and it works

Now i try to create a loop


This Discussion