Archive for the ‘TCL’ Category

TCL Tips

August 22, 2013 7 comments

Note: Credits for the tips mentioned here goes to multiple people
and internet sites.  I just consolidated them together for my reference.

Hex Codes for special key combinations to be send to routers/PC

Refer the following table for the hexa decimal code for each of the special key combinations. Most frequently used one is CTRL+C. This can be used in tcl/expect to send these key combinations to a router/PC.

KeyCombination Hexadecimal
CTRL-@ 0x00
CTRL-A 0x01
CTRL-B 0x02
CTRL-C 0x03
CTRL-D 0x04
CTRL-E 0x05
CTRL-F 0x06
CTRL-G 0x07
CTRL-H 0x08
CTRL-I 0x09
CTRL-P 0x10
CTRL-Q 0x11
CTRL-R 0x12
CTRL-S 0x13
CTRL-T 0x14
CTRL-U 0x15
CTRL-V 0x16
CTRL-W 0x17
CTRL-X 0x18
CTRL-Y 0x19

Dynamic variables in Tcl (Eval command to the rescue)

Most often I ran in to issue how to reference or retrieve value from a dynamic tcl variable (or value of a variables, variable)..Got confused!!!? let me explain with an example what the problem is and whats the solution?

set vlanid "11 12 13"
set iplst " 12.0.1 13.0.1"
#Now you want to have the below mapping
#vlan 11->
#vlan 12->
#vlan 13->
#Now lets say i have to store the ip of vlan 11 in a name referenced by the vlan id like below

foreach id $vlanid ip $iplst {
set vlan$id $ip

#Now we have the following variable to value mapping
#vlan11 -->
#vlan12 -->
#vlan13 -->
#The above is called dynamic variable as the variable name is dynamically changing

#Now comes the real question, how to get the value of a dynamic variable
#I cannot use puts "$vlan$id" as it will error out saying cannot read "vlan" no such variable.
#This is where the power of eval comes in to picture and below is the solution

foreach i $vlanid {
puts "The ip of vlan $i is [eval set vlan$i]"

The above code will give the output

The ip of vlan 11 is
The ip of vlan 12 is
The ip of vlan 13 is

Categories: TCL, Technical