Thursday, August 7, 2008

Sample: Useful proc in TCL

a, begin
#!/usr/bin/expect --
if {$argc == 0} {
puts stderr "Usage: changelink up|down"
exit 1
} else {
set status [lindex $argv 0]
}


b, check if the string exactly match. linkup and linkdown are the proc.

if { [string equal $status up] } {
linkup
} else {
linkdown
}

c, proc
proc linkdown { } {
spawn telnet 172.18.9.26
expect "#"
send "conf t\r"
expect "#"
send "inter f 0/11\r"
expect "#"
send "shut\r"
send "end\r"
#expect "#"
send "exit\r"
}

d, proc with parameter
proc clear_line {line} {
spawn telnet 172.18.9.29
expect "#"
send "clear line $line\r"
expect "confirm"
send "y\r"
exec sleep 2
expect "#"
send "exit\r"
expect "#"
}

e, proc used for firewall test

proc clear_line {line} {
spawn telnet 172.18.9.29
expect "#"
send "clear line $line\r"
expect "confirm"
send "y\r"
exec sleep 2
expect "#"
send "exit\r"
expect "#"
}

proc login_fw {2kline} {
spawn telnet 172.18.9.29 $2kline
#exec sleep 1
expect "]"
send "\r"
exec sleep 5
expect {
"login" {send "admin\r"
expect "assword"
send "\r"}
"#" {send "end\r"}
"assword" {send "aaa\r"
expect "login"
send "admin\r"
expect "assword"
send "\r"}
expect "#"
}
return $spawn_id
}

proc print_csum {serial} {
set spawn_id [login_fw $serial]
expect "#"
send "diag sys ha sh\r"
#exec sleep 5
expect "#"
set csumA $expect_out(buffer)
puts $csumA
}


clear_line $h3600
print_csum [expr 2000 + $h3600]

f, multispawn interactive

spawn telnet 172.18.9.29 200$h3600
set id1 $spawn_id
expect "]"

spawn telnet 172.18.9.29 200$l3600
set id2 $spawn_id
expect "]"

proc login_fw_id1 { } {
global id1
set spawn_id $id1
send "\r"
exec sleep 5
expect {
"login" {send "admin\r"
expect "assword"
send "\r"
expect "#"
}
"#" {send "end\r"
expect "#"}
"assword" {send "aaa\r"
expect "login"
send "admin\r"
expect "assword"
send "\r"}
expect "#"
}
return $spawn_id
}

proc login_fw_id2 { } {
global id2
set spawn_id $id2
send "\r"
exec sleep 5
expect {
"login" {send "admin\r"
expect "assword"
send "\r"
expect "#"
}
"#" {send "end\r"
expect "#"}
"assword" {send "aaa\r"
expect "login"
send "admin\r"
expect "assword"
send "\r"}
expect "#"
}
return $spawn_id
}

proc GetStatus-1 { } {
set spawn_id [login_fw_id1]
send "get sys status\r"
expect "#"
}


proc GetStatus-2 { } {
set spawn_id [login_fw_id2]
send "get sys status\r"
expect "#"
}


GetStatus-1
GetStatus-2

g, generate config file

#!/usr/bin/tclsh
set fileId [open ./1000vip.cfg w]
puts $fileId "config firewall vip"
for {set i 0} {$i < 4} {incr i} {
for {set j 1} {$j < 254} {incr j} {
set v [expr 254*$i+$j]
puts $fileId "edit jkvip$v
set extip 1.1.$i.$j
set mappedip 2.2.$i.$j
set extintf port1
next"
}
}
puts $fileId "end"

puts $fileId "config firewall vip"
for {set k 1} {$k <1001} {incr k} {
puts $fileId "delete jkvip$k"
}
puts $fileId "end"
close $fileId

h, send command from config file
proc sendcom {serial} {
set spawn_id [login_fw $serial]
expect "#"
send "conf g\r"
expect "#"
send " d debug cli 5\r"
expect "#"
set fileId [open ./250_intf_noip.cfg]
foreach line [split [read $fileId] \n] {
send "$line\r"
expect "#"
}
}
sendcom [expr 2000 + $line]

proc psleep {m} {
for {set n 0} {$n < [expr $m + 1]} {incr n 10} {
exec sleep 10
puts "$n...."
}
}

if {[string first "backup" $mainstatus] !=-1} { if mainstatus 中包括backup
puts " ################## Case 1: Everything same but SN.... Works #####################"
puts " ################## Case 1: L3600 with Higher SN wins #####################"
} else {
puts " ################## Case 1: Everything same but SN.... Fail #####################"
}


i, split a list and get one item which contain the certain string

proc print_csum {serial} {
set spawn_id [login_fw $serial]
expect "#"
send "diag sys ha sh\r"
#exec sleep 5
expect "#"
set csumA $expect_out(buffer)
puts $csumA
set l1 [split $csumA \r]
return [lsearch -inline $l1 *all*]
}

j, send command through telnet
spawn telnet 172.18.9.150
expect "login"
send "admin\r"
expect "assword:"
send "1\r"
expect "#"
while {1} {
set fileId [open $filea]
foreach line [split [read $fileId] \n] {
send "$line\r"
expect "#"
}
close $fileId


k, foreach
foreach value {126 125} {
spawn telnet 172.18.9.$value
expect "login"
send "admin\r"
expect "assword:"
send "\r"
expect "#"


============
How to generate random number between 0 and 100
set value [expr floor(rand()*100)]
puts "$value"
sleep $value
}

proc ransleep { } {
set value [expr floor(rand()*100)]
puts "$value"
sleep $value
}

No comments: