有时候,当我孤独地坐着等待生命大门关闭时,一种与世隔绝的感觉就会像冷雾一样笼罩着我。远处有光明、音乐和友谊,但我进不去,命运之神无情地挡住了大门。我真想义正词严地提出抗议,因为我的心仍然充满了热情。但是那些酸楚而无益的话语流溢在唇边,欲言又止,犹如泪水往肚里流,沉默浸透了我的灵魂。然后,希望之神微笑着走来对我轻轻耳语说:“忘我就是快乐。”因而我要把别人眼睛所看见的光明当作我的太阳,别人耳朵所听见的音乐当作我的乐曲,别人嘴角的微笑当作我的快乐。

TCL脚本学习记录

上一篇 / 下一篇  2009-07-27 11:26:14 / 个人分类:C语言

对于TCL脚本,摘录后进行分析:注释部分是自己填写,希望有所收获:

proc ping_all {ip} {
if {[regexp "(!!)" [exec "ping $ip"]]} {

#regexp ?switchs? ?--? exp string ?matchVar?\ ?subMatchVar #subMatchVar...?

# regexp 用于判断正则表达式是否全部或者部分匹配string 部分,匹配返回1,不匹配返回0;


set counter 0 

#counter是计数器
exec "term len 0"
set hostname [lindex [exec rsh $ip "sho run | include hostname"] 1]

# 设置用户名是执行了命令 sho run 命令后结果中返回值中包含hostname 的第一项元素。
set int [exec rsh $ip "show ip int brief"]

# int 是执行这个命令的结果。ip地址

set length [llength $int]

返回int (IP地址)的元素长度length

while {$counter<=$length} {
set tmp [lindex $int $counter]

#返回ip地址的第counter 个元素。也就是第几个IP地址。

if {[regexp "(^\[0-9]+\.\[0-9]+\.\[0-9]+\.\[0-9]+)" $tmp ]} {

#对tmp的ip地址进行多次匹配,匹配成功返回1. ”.“表示匹配任意单个字

#符。'^"表示从头进行匹配。
  puts "\n"
  puts "**********************************"
  puts "* Ping $hostname [lindex $int [expr $counter - 1]]"

#ping 下一个IP地址。
  puts "**********************************"
  exec "ping $tmp"
  }
incr counter
}
} else {
  puts "\n\n"
  puts "IP address $ip is not reachable\n"
  set route [exec "show ip route $ip"]
  puts "Output from the show ip route command\n"
  puts "[exec "show ip route $ip"]"
}
}

Script. Breakdown :
The script. reads in the IP address of the device you want to
test reachability to. The script. then tests to ensure that is
can reach that particular IP address by trying to ping the IP
address. If the ping is unsucessfull an error message is
displayed like below:

脚本读取测试可达性的设备IP地址。然后通过ping IP地址检测是否可达。如果ping失败,出现的错误如下所示:

Rack1R1#ping_all 150.1.4.4


IP address 150.1.4.4 is not reachable

Output from the show ip route command

% Subnet not in table

Rack1R1#

If the ping is sucessful the script. RSH's to the remote device
and discovers it's hostname along with all the IP addresses that
are assigned to the interfaces of the remote device. Next the
script. attempts to ping the IP addresses of the remote router from
the local router.

如果PING成功。rsh远程,发现主机名,和在远程设备各个端口的IP地址。下一步,试图通过本地路由器ping 远程路由器。

Implementation:

Rack1R1#tclsh
Rack1R1(tcl)#
Rack1R1(tcl)#proc ping_all {ip} {
+>
+> if {[regexp "(!!)" [exec "ping $ip"]]} {
+>
+>   set counter 0
+>   exec "term len 0"
+>   set hostname [lindex [exec rsh $ip "sho run | include hostname"] 1]
+>   set int [exec rsh $ip "show ip int brief"]
+>   set length [llength $int]
+>
+>   while {$counter<=$length} {
+>     set tmp [lindex $int $counter]
+>
+>     if {[regexp "(^\[0-9]+\.\[0-9]+\.\[0-9]+\.\[0-9]+)" $tmp ]} {
+>       puts "\n\n"
+>       puts "**********************************"
+>       puts "*  Ping $hostname [lindex $int [expr $counter - 1]]"
+>       puts "**********************************"
+>       exec "ping $tmp"
+>     }
+>     incr counter
+>   }
+> } else {
+>          puts "\n"
+>          puts "IP address $ip is not reachable\n"
+>          set route [exec "show ip route $ip"]
+>          puts "Output from the show ip route command\n"
+>          puts "[exec "show ip route $ip"]"
+> }
+>}

Rack1R1(tcl)#ping_all 150.1.2.2



**********************************
*  Ping Rack1R2 Ethernet0/0
**********************************

Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 10.1.1.2, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 1/2/4 ms


**********************************
*  Ping Rack1R2 Loopback0
**********************************

Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 150.1.2.2, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 1/2/4 ms
Rack1R1(tcl)#


TAG:

 

评分:0

我来说两句

Open Toolbar