Cisco Support Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Announcements
Webcast-Catalyst9k
New Member

TCL script for manipulating interfaces

Hello,

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; }
  return;
}

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.

13 REPLIES
Cisco Employee

Re: TCL script for manipulating interfaces

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
New Member

Re: TCL script for manipulating interfaces

JClarke,

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.

Cisco Employee

Re: TCL script for manipulating interfaces

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?

New Member

Hi,

Hi,

Having problems running this on devices with ports in the format of

interface-number = network-module-slot/interface-card-slot/port

I get the error message on an IOS device with a port of gi1/0/1

No such interface: gi1/0/1

works fine on port fa0/1 say

Cisco Employee

The EEM script should work

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

New Member

I was able to run it

I was able to run it successfully on a 3640 router on GNS3 but had the error when running it on a "real" switch (3650).

I used EEM to allow me to remotely shut down and interface and have it automatically "un shut" after minute.

Will revisit this again out of curiosity

New Member

Re: TCL script for manipulating interfaces

This will be exclusively ran on Cisco 2800s running 12.4.24.T1.

Cisco Employee

Re: TCL script for manipulating interfaces

Here is the script rewritten for EEM.  You can still alias it, too:

alias exec ifchange event manager run no_ifchange.tcl

New Member

Re: TCL script for manipulating interfaces

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.

New Member

TCL script for manipulating interfaces

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

Cisco Employee

TCL script for manipulating interfaces

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.

New Member

TCL script for manipulating interfaces

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

New Member

TCL script for manipulating interfaces

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

1296
Views
5
Helpful
13
Replies
CreatePlease to create content