Cisco TCL Tips
 
Cisco TCL tips and tricks
 
And here for Starting:
the "Hello World" sample
	evil-router#
	evil-router#tclsh
	evil-router(tcl)#puts "Hello World\n"
	Hello World
	
	
	evil-router(tcl)#tclquit
	evil-router#
Or put the following Line in a File on Your TFTP Server with the filename "HelloWorld.tcl"
	puts "hello world";
and execute the tclsh on your Router.
evil-router#tclsh tftp://192.168.1.100/HelloWorld.tcl

Loading HelloWorld.tcl from 192.168.1.100 (via FastEthernet0): !
[OK - 23 bytes]
hello world


evil-router#  	
Informations
 Cisco TCL Infos (PDF)

TCL Script Installation
 

 
download the file YOURSCRIP.tcl into flash:YOURSCRIPT.tcl
configure a alias: alias exec YOURSCRIPT tclsh flash:YOURSCRIPT.tcl
execute with: YOURSCRIPT [option1] [option2]
Or you place all the scripts on an central TFTP Server, then you can create a alias to the TFTP Server. Each time
you execute the script, it's donwloadet from the tftp server
configure a alias: alias exec YOURSCRIPT tclsh tftp:TFTP_IP_ADDRESSE/YOURSCRIPT.tcl
Very good for development,
 
Sample
 evil-router#
 evil-router#copy tftp://192.168.2.100/tcl/cnc.tcl flash:cnc.tcl
 Destination filename [cnc.tcl]?
 Accessing tftp://192.168.2.100/tcl/cnc.tcl...
 Loading tcl/cnc.tcl from 192.168.2.100 (via FastEthernet0): !!
 [OK - 9300 bytes]
 
 9300 bytes copied in 0.256 secs (36328 bytes/sec)
 evil-router#conf t
 Enter configuration commands, one per line.  End with CNTL/Z.

 evil-router(config)#alias exec cnc tclsh flash:cnc.tcl
 evil-router(config)#exit
 evil-router#sh alias
 Exec mode aliases:
   p                     ping
   r                     resume
   s                     show
   w                     where
   scanip                tclsh flash:scanip.tcl
   cnc                   tclsh flash:cnc.tcl
 
 evil-router#cnc -v
 cnc.tcl version 0.08
 (c) 2008 packetlevel.ch / 05.10 2008

ANSI Color Output
 
If you try to generate ansi escape sequences, you can use "term intern" on exec level
 
For example, to clear the screen and display red heading text, use the following commands:  
	exec terminal international;
	puts "\033\[2


J\033\[H\033\[1;31mHeader text\033\[m"
 
Other Example:
 
	proc fahne { } {
	exec "terminal international"
	puts "\033\[2J"
	puts "\033\[0;0H"
	puts "\033\[0m |##########"
	puts "\033\[0m |#########"
	puts "\033\[0m |\033\[31m########"
	puts "\033\[0m |\033\[31m########"
	puts "\033\[0m |\033\[33m#########"
	puts "\033\[0m |\033\[33m##########"
	puts "\033\[0m |"
	puts "\033\[0m |"
	puts "\033\[0m |"
	puts "\033\[0m\n\n\033\[32m Yepp \033\[0m"
	}
	fahne	
 
Sample Output
 
 
For example, to clear the screen and display red heading text, use the following commands:  
	exec terminal international;
	puts "\033\[2J\033\[H\033\[1;31mHeader text\033\[m"

SNMP with Tcl
 
SNMP with Tcl
 
for the following sample, your SNMP RO Community must be "public"
	snmp-server community public ro
 
the following script prints the router uptime.
 
	#
	# Simple Tcl script to print system uptime
	#
	set value [snmp_getone test system.3.0]
	regexp {oid='(.*)'.*val='(.*)'} $value ignore oid result
	set result [expr $result / 100]
	puts "Router uptime is $result seconds"

ios_config + exec
 
With ios_config, you can execute ios command direct.
 
exec command to execute an exec-level command, for example exec "show ip route"
ios_config mode command to configure the router
 
Sample
	ios_config "interface fastethernet 2" "description Uplink2ISP" "duplex full"
configure the interface fastethernet with a desrition an FullDuplex
Informations

Tclsh command line parameters
 
Tclsh command line parameters
 
Every word you enter after the file name in the tclsh command line is passed as a parameter to the Tcl code you execute. To get these parameters in Tcl, use Tcl commands similar to the code below:
	# loop.tcl: changes loopback state
	#
	# syntax: tclsh loop.tcl ifnum state
	#
	set ifnum [lindex $argv 0] # first parameter after file name
	set ifstate [lindex $argv 1] # second parameter after file name
	if {[string equal $ifstate ""]} {
	return -code error "Syntax: loop.tcl ifnum ifstate"
	}
	... rest of procedure .;

Insert responses to command prompts in Tclsh
 
Insert responses to command prompts in Tclsh
 
	typeahead "y"
	exec "clear counter dialer 0";
 
Other Typeahead Samples
 
use "\n" for submit
	typeahead "test dsp\n"
	typeahead "set auto_recovery none\n"
	typeahead "set auto_recovery tdm\n"
	typeahead "stop dsp 1 2 3 1\n"
	typeahead "stop dsp 1 2 3 2\n"
	typeahead "sh pool\n"
	typeahead "q\n"
		
              

(c) 2008 by packetlevel.ch / last update: 05.10.2008