I have been spending a little time working with TCL, a scripting language which is open source but is also available in the majority of Cisco's IOS. If you would like more information, check out Cisco Press - TCL Scripting for Cisco IOS.
TCL Scripting (Some of the Basics)
R1(tcl)#
set a "This is"
set b " a test!"
Router(tcl)#append a $b
This is a test!
______________
r1(tcl)#set x 1
1
r1(tcl)#incr x
2
r1(tcl)#set y 2
2
r1(tcl)#incr y
3
r1(tcl)#incr y 2
5
______________
r1(tcl)#set m 2
2
r1(tcl)#expr $m+2
4
r1(tcl)#puts $m
2
______________
" " Double quotes allow substitutions
{ } Braces need to be open / closed and DONT allow substitutions.
[ ] This is for command substitution and invokes the TCL interpreter to process the characters between the open and closed brackets.
This allows for an array variable
$variable
$variable (index)
r1(tcl)#set x(2) 200
200
r1(tcl)#set y(3) 300
300
r1(tcl)#puts $x(2)
200
r1(tcl)#set n(m) 1000
1000
r1(tcl)#puts $n(m)
1000
r1(tcl)#set {I am awesome!} Adrian
Adrian
r1(tcl)#set c cool
cool
r1(tcl)#set d "I am $c"
I am cool
r1(tcl)#puts {You are $c}
You are $c
______________
Append is similar to lappend but the variable's are contained in quotes.
r1(tcl)#set a "This is "
This is
r1(tcl)#set b "my script"
my script
r1(tcl)#append a $b
This is my script
r1(tcl)#puts $a
This is my script
______________
List Append (lappend) add's a variable to a string / list separated by white space.
r1(tcl)#lappend oscar This is
This is
r1(tcl)#lappend oscar my script
This is my script
r1(tcl)#puts $oscar
This is my script
______________
List Index (lindex) will extract elements from a list but will not modify the list.
As you can see we have extracted data from this list. Keep in mind that the list is read from left to right and start's at the number zero.
r1(tcl)#puts $a
This is my script
r1(tcl)#lindex $a 2
my
r1(tcl)#
r1(tcl)#lindex $a 0
This
r1(tcl)#puts $a
This is my script
______________
List Insert (linsert) will add new elements to a list.
r1(tcl)#set a [linsert $a 3 awesome ]
This is my awesome script
r1(tcl)#
r1(tcl)#puts $adrian
This is my awesome list
r1(tcl)#llength $adrian
5
______________
r1(tcl)#lsearch $adrian y
-1
r1(tcl)#lsearch $adrian my
2
r1(tcl)#lsearch -regexp $adrian y
2
r1(tcl)#lsearch -regexp $adrian i
0
r1(tcl)#lsearch -global $adrian i
bad search mode "-global": must be -exact, -glob, or -regexp
r1(tcl)#lsearch -glob $adrian i
-1
r1(tcl)#puts $adrian
This is my list
r1(tcl)#lsearch -regexp $adrian "is"
0
r1(tcl)#
______________
r1(tcl)#set a "This is my script"
This is my script
r1(tcl)#puts $a
This is my script
______________
r1(tcl)#set a [lreplace $a 3 3 really awesome script]
This is my really awesome script
r1(tcl)#puts $a
This is my really awesome script
r1(tcl)#set b "My dogs name is oscar"
My dogs name is oscar
r1(tcl)#set b [lreplace $b 0 4 I also have a dog named shelby!]
I also have a dog named shelby!
r1(tcl)#
______________
r1(tcl)#puts $c
Pulling info from a file
r1(tcl)#set d [lrange $c 2 4]
from a file
r1(tcl)#puts $d
from a file
______________
List Sort puts a string of variables in alphabetical order.
r1(tcl)#puts $d
from a file
r1(tcl)#set d [lsort $d]
a file from
r1(tcl)#lsort -ascii -decreasing $d
from file a
r1(tcl)#lsort -ascii -increasing $d
a file from
r1(tcl)#
______________
r1(tcl)#proc my_script {} {
+>(tcl)#puts "This is my script"
+>(tcl)#}
r1(tcl)#set z {}
r1(tcl)#puts $z
r1(tcl)#for {set z 0} {$z<10} {incr z} {
+>(tcl)#my_script
+>(tcl)#}
This is my script
This is my script
This is my script
This is my script
This is my script
This is my script
This is my script
This is my script
This is my script
This is my script
______________
r1(tcl)#for {set m 0} {$m<5} {incr m} {puts " $m. This is a script"}
0. This is a script
1. This is a script
2. This is a script
3. This is a script
4. This is a script
r1(tcl)#set cpuinfo {r1 50 90 r2 10 20}
r1 50 90 r2 10 20
r1(tcl)#$info { set CPUavg [expr ($CPU1+$CPU2)/2] ; puts "$router $CPUavg" }
r1 70
r2 15
______________
r1(tcl)#set y 0 ; while {$y < 5} { set T [expr ($y*2)] ; puts "$y. Twice $y is $T" ; incr y }
0. Twice 0 is 0
1. Twice 1 is 2
2. Twice 2 is 4
3. Twice 3 is 6
4. Twice 4 is 8
More to come...
No comments:
Post a Comment