1、TCL 脚本实例: 检查 IP 的脚本 1 脚本源码 本脚本系谭勇书写运行结果如下:rootMeil89 bin# ./IP.tcl -f /cfg/ip_R2.6.0_DRM.cfg= Ping 10.50.87.13 failed! = Ping 10.50.86.16 failed! = Ping 10.50.86.17 failed! = Ping 10.50.86.39 failed! = Ping 10.50.86.16 failed! = Ping 10.50.86.17 failed! = Ping 10.50.86.39 failed! = Ping 10.56.50.21
2、failed! =invalid IP 10.56.53/31Invalid ip string: 10.56.53/31!= Totally 52 ping successfully as below:10.50.87.1010.50.87.1110.50.87.1210.50.87.1610.50.86.1010.50.86.1110.50.86.1210.50.86.1310.50.86.1810.50.86.2110.50.86.2210.50.86.2510.50.86.2810.50.86.3110.50.86.3210.50.86.3510.50.86.3610.50.53.22
3、10.56.90.10010.56.90.10110.50.86.1010.50.86.1110.50.86.1210.50.86.1310.50.86.1810.50.86.2110.50.86.2210.50.86.2510.50.86.2810.50.86.3110.50.86.3210.50.86.3510.50.86.3610.56.50.2010.56.50.3010.56.50.3110.56.90.1310.56.90.1410.56.90.1010.56.90.1110.56.90.1710.56.90.1810.56.90.5010.56.90.5110.56.90.701
4、0.50.12.3310.56.50.1710.56.50.1810.56.50.6010.56.53.2010.56.53.1010.56.53.11= Can not ping 8 host as below:10.50.87.1310.50.86.1610.50.86.1710.50.86.3910.50.86.1610.50.86.1710.50.86.3910.56.50.21rootMeil89 bin# rootMeil89 bin# ./IP.tcl ping 10.50.100.51-70= Ping 10.50.100.53 failed! = Ping 10.50.100
5、.54 failed! = Ping 10.50.100.57 failed! = Totally 17 ping successfully as below:10.50.100.5110.50.100.5210.50.100.5510.50.100.5610.50.100.5810.50.100.5910.50.100.6010.50.100.6110.50.100.6210.50.100.6310.50.100.6410.50.100.6510.50.100.6610.50.100.6710.50.100.6810.50.100.6910.50.100.70= Can not ping 3
6、 host as below:10.50.100.5310.50.100.5410.50.100.57rootMeil89 bin# 今后我们将对这个脚本进行学习.# !/usr/bin/tclsh# !/bin/bash# exec tclsh “$0“ “$“set auto_path lappend auto_path file dirname info script /libpackage require ftppackage require Expectset Usage “Usage: n ttclsh $argv0 -f iplist.cfg ?blockn ttclsh $ar
7、gv0 ping startIp-endIpn ntExample: n tttclsh $argv0 -f iplist.cfg CMS n tttclsh $argv0 ping 10.50.97.60-65n“# Define the global variablesset failPing set pingSuccess set flag “no“set ipListFile “set ipBlock “set ipList set tmpArgv split string trim join $argv “ “if llength $tmpArgv = 2 set flag lind
8、ex $tmpArgv 0if lindex $tmpArgv 0 = “-f“ set ipListFile lindex $tmpArgv 1if llength $tmpArgv = 3 set ipBlock lindex $tmpArgv 2 elseif string equal -nocase $flag “ping“ lappend ipList list “NULL“ lindex $tmpArgv 1 else puts $Usageexit else puts $Usageexit# Get the host listif $ipListFile != “ if $ipB
9、lock = “ set listBlock getProfile $ipListFile “ALL“ else set listBlock $ipBlockforeach block $listBlock if string equal -nocase $block “error“ | string equal -nocase $block “args“ continueset listKey getProfile $ipListFile $block “ALL“ “ONLY_KEY“# Insert the key to the ipList like: blockName KeyName
10、foreach key $listKey if string equal -nocase $key “error“ continue lappend ipList list $block $key# Handle each listforeach ip_list $ipList set block lindex $ip_list 0set ipStr lindex $ip_list 1set ipKey $ipStrif $block != “NULL“ set ipStr getProfile $ipListFile $block $ipKeyif string equal -nocase
11、$ipStr “error“ continue # Get the IP according to the ipStrif set g_ipList GetIP $ipStr = -1 puts “Invalid ip string: $ipStr!“continue# Ping the IPset result “# BadIp: “set num 0foreach ip $g_ipList # Ping the hostif PingHost $ip 1 != 0 lappend failPing $ipincr numregexp (0-9+).(0-9+).(0-9+).(0-9+)
12、$ip - p1 p2 p3 p4append result “ $p4“puts “= Ping $ip failed! =“ else lappend pingSuccess $ip#puts “= Ping $ip successfully! =“# Write the result if $block != “NULL“ if $num = 0 set result “# OK“set tmpStr format “%-30s%s“ string trimright $ipStr $resultsetProfile $ipListFile $block $ipKey $tmpStrif llength $pingSuccess = 1 puts “n=nTotally llength $pingSuccess ping successfully as below:“foreach tmp $pingSuccess puts “t$tmp“# Print the resultif llength $failPing = 1 puts “n=nCan not ping llength $failPing host as below:“foreach tmp $failPing puts “t$tmp“