12-08-2009 12:38 PM
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:
I do not have any experience in TCL and very little in EEM. Any and all assistance would be greatly appreciated. Thanks again.
12-08-2009 01:20 PM
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
12-08-2009 01:31 PM
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.
12-08-2009 01:37 PM
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?
02-03-2016 09:42 AM
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
02-03-2016 09:52 AM
The EEM script should work with any interface. I don't see any changes that would be necessary for switch/mod/slot configs.
02-03-2016 12:37 PM
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
12-08-2009 01:49 PM
This will be exclusively ran on Cisco 2800s running 12.4.24.T1.
12-08-2009 09:02 PM
12-13-2009 06:29 AM
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.
02-13-2014 02:23 AM
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
02-13-2014 10:05 AM
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.
02-14-2014 01:10 AM
And how is that possible to put the time from the clock in a variable ?
02-14-2014 02:14 AM
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
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide