Use Tcl script to change the interface status

During network troubleshooting or proof-of-concept testing, I often change the state of a loopback interface (to insert or remove IP prefix from the routing protocols) or flap it to test the impact on network stability. The traditional approach to this procedure should be known to everyone:
  • Enter configuration mode;
  • Select the appropriate interface with the interface loopback x command;
  • Try to remember whether you need to disable or enable it;
  • Issue the shutdown or no shutdown command;
  • Exit the configuration mode and continue your debugging/testing process.

After a particularly boring testing session I decided to write a Tcl script to automate the job. To use it, download it and store it into ifchange.tcl. Download the Tcl file to your router (Flash or NVRAM) and define an alias: alias exec ifchange tclsh flash:ifchange.tcl. Now you can use the new ifchange command to change interface status.

A sample router printout is included below:
a1#ifchange
Syntax: tclsh ifchange.tcl interface [on|off|change|flap]
a1#ifchange loop0 off
Interface loop0 changed state to off
a1#
%LINK-5-CHANGED: Interface Loopback0, changed state to administratively down
%LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback0, changed state to down
a1#ifchange loop0 on
Interface loop0 changed state to on
a1#
%LINK-3-UPDOWN: Interface Loopback0, changed state to up
%LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback0, changed state to up
a1#ifchange loop0 flap
changing state of loop0
shut down interface loop0
... waiting ...
%LINK-5-CHANGED: Interface Loopback0, changed state to administratively down
%LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback0, changed state to down
changing state of loop0
enable interface loop0
a1#
%LINK-3-UPDOWN: Interface Loopback0, changed state to up
%LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback0, changed state to up

2 comments:

  1. Hi Thanks for the post. I have your script file(ichange.tcl) on my TFTP server. I am trying to download the file on flash using copy "tftp://tftp_ip_address/ifchange.tcl run " command from my router, however it doesnt seem to get copy.Giving below error, I am not sure if I am doing anything wrong, please help.
    Accessing tftp://192.168.2.6/ifchange.tcl...
    Loading ifchange.tcl from 192.168.2.6 (via FastEthernet0/0): !
    [OK - 1658 bytes]

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

    % Invalid input detected at '^' marker.

    proc doConfig { mode cmd } {
    ^
    % Invalid input detected at '^' marker.

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

    % Invalid input detected at '^' marker.

    }
    ^
    % Invalid input detected at '^' marker.

    proc getState { ifnum } {
    ^
    % Invalid input detected at '^' marker.

    if { [ catch { set ifstate [exec "show interface $ifnum"] } iferror ] } {
    ^
    % Invalid input detected at '^' marker.

    error "No such interface: $ifnum";
    ^
    % Invalid input detected at '^' marker.

    }
    ^
    % Invalid input detected at '^' marker.

    set result [expr [ string first {administratively down} $ifstate ] < 0]
    ^
    % Invalid input detected at '^' marker.

    return $result ;
    ^
    % Invalid input detected at '^' marker.

    }
    ^
    % Invalid input detected at '^' marker.

    proc changeState { ifnum } {
    ^
    % Invalid input detected at '^' marker.

    puts "changing state of $ifnum" ;
    ^
    % Invalid input detected at '^' marker.

    if { [getState $ifnum] } {
    ^
    % Invalid input detected at '^' marker.

    puts "shut down interface $ifnum" ;
    ^
    % Invalid input detected at '^' marker.

    doConfig "interface $ifnum" "shutdown"; } else {
    ^
    % Invalid input detected at '^' marker.

    puts "enable interface $ifnum" ;
    ^
    % Invalid input detected at '^' marker.

    doConfig "interface $ifnum" "no shutdown"; }
    ^
    % Invalid input detected at '^' marker.

    }
    ^.
    .
    .
    .
    .
    .


    } errmsg ] } { puts stderr $errmsg; }
    ^
    % Invalid input detected at '^' marker.

    1658 bytes copied in 4.220 secs (393 bytes/sec)
    R2#
    *Mar 1 00:08:12.967: %PARSER-4-BADCFG: Unexpected end of configuration file.
  2. my bad!!! what was i thinking...al good now Ivan.. ignore my previous comment.
    copy tftp://192.168.2.6/ifchange.tcl flash:
    cheers :)
Add comment
Sidebar