Tuesday, October 18, 2011

TCL Notes - Part 2

This is an example of a simple script as well as how to run them from either the TCL shell or global exec mode. I will have an example of how to fire off a TCL script using EEM at a later date.

Let's get started -

The command below get's passed off to IOS because the TCL interpreter doesn't understand what to do with it, thus the output looks like it was from global exec. It does populate the variable "mybuffer" with the output seen.


2811_Home(tcl)#set mybuffer [exec "show ip interface brief"]
Load for five secs: 2%/0%; one minute: 2%; five minutes: 3%
Time source is NTP, 20:10:08.650 CST Tue Oct 18 2011

Interface                  IP-Address      OK? Method Status                Protocol
FastEthernet0/0            192.168.1.1     YES NVRAM  up                    up    
FastEthernet0/1            10.1.2.1        YES NVRAM  up                    up    
FastEthernet0/1.3          10.1.3.1        YES NVRAM  up                    up    
Dot11Radio0/0/0            unassigned      YES NVRAM  up                    up    
NVI0                       192.168.1.1     YES unset  up                    up  
 
What this script is doing is looking for the first instance of "10.1.2." in the variable "mybuffer" which was populated by "show ip interface brief" command. If found, it will return the line "We found my inside subnet 10.1.2.0 / 24!" and if not it will return a (-1).

Here is the complete script, written in a simple text editor and saved as a ".tcl" file.

set mybuffer [exec "show ip interface brief"]
set foundposition [string first "10.1.2." $mybuffer]
if {$foundposition > -1} {
puts "We found my inside subnet 10.1.2.0 / 24!"
}

I copied the file to my router...

2811_Home#copy usbflash0: flash
Source filename []? my-tcl.tcl
Destination filename [my-tcl.tcl]?
Copy in progress...C
181 bytes copied in 0.416 secs (435 bytes/sec)


2811_Home#sh flash
Load for five secs: 11%/0%; one minute: 4%; five minutes: 3%
Time source is NTP, 20:19:55.263 CST Tue Oct 18 2011
-#- --length-- -----date/time------ path
1     59171892 Oct 14 2011 06:08:04 c2800nm-adventerprisek9_ivs-mz.124-22.T5.bin
33        5561 Aug 27 2011 20:10:16 2811_internet_ips-CONFIG
35    12757876 Sep 07 2011 02:55:20 IOS-S556-CLI.pkg
42         180 Oct 20 2011 01:31:54 my-tcl.tcl

Here are (2) ways in which to execute to script.

2811_Home#tclsh
2811_Home(tcl)#source flash:my-tcl.tcl
We found my inside subnet 10.1.2.0 / 24!

2811_Home#tclsh flash:my-tcl.tcl
We found my inside subnet 10.1.2.0 / 24!

Thats it for now...

No comments:

Post a Comment