Interface status change from IOS exec prompt
From CT3
The default IOS user interface allows you to change interface status only from the router configuration mode, forcing the operators performing network troubleshooting or testing to switch between the exec prompt (where they execute show commands) to interface configuration mode (where they can issue the [ no ] shutdown command). This tclsh script allows you to change the interface status directly from the exec prompt.
Contents |
Installation
- Download the source file into flash:ifchange.tcl.
- Configure alias exec ifchange flash:ifchange.tcl.
Usage guidelines
Usage: ifchange interface-name on|off|change|flap
Command line parameters:
- interface-name: The target interface. Do not use spaces in interface names. Any interface name abbreviation recognized by Cisco IOS (for example, Fa0 for FastEthernet0) can be used. Interface names are not checked by the script, it will simply fail if you supply invalid interface name.
- Target status:
- on/off: enable or disable target interface.
- change: change the interface status (on to off or vice versa).
- flap: disable the interface, wait for 6 seconds and re-enable the interface (shorter flaps cannot be generated due to interface state change suppression code in Cisco IOS).
Author
Ivan Pepelnjak, © 2007 NIL Data Communications
Source code
#
# Copyright (c) 2007 NIL Data Communications
# All rights reserved.
#
# by: Ivan Pepelnjak, NIL Data Communications
# title: Simplified interface state change
# name: ifchange.tcl
# desc: This script disables, enables or flaps an interface from the IOS exec prompt
#
# ios config:
#
# download the file into flash:ifchange.tcl
# configure alias exec ifchange tclsh flash:ifchange.tcl
#
# invoke with ifchange interface-name on|off|change|flap
#
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; }
BlogMarks
del.icio.us
digg
Facebook
LinkedIn
Newsvine
reddit
Slashdot