TCL (工具控制语言) 是一种scripting 语言由CISCO 系统extensiviely 使用对facilited 测试各种各样的路由器和开关平台。
RSH (遥远的壳) 使用执行命令在一个远程设备。RSH 最初被开发了用于UNIX enviroment 缓和多个机器的管理。RSH 极端不安全和不能常用。RSH 被发现在Cisco IOS 和在几乎所有Linux/UNIX 发行。
硬件配置:
2 台连接到Ethernet 的Cisco 路由器
IOS version 12.2T
配置:
R1:
version 12.2
!
hostname Rack1R1
!
no ip domain lookup
!
interface Loopback0
ip address 150.1.1.1 255.255.255.0
!
interface Ethernet0/0
ip address 10.1.1.1 255.255.255.0
!
router rip
version 2
network 10.0.0.0
network 150.1.0.0
no auto-summary
!
line con 0
line aux 0
line vty 0 4
password cisco
login
!
end
R2:
version 12.2
!
hostname Rack1R2
!
ip rcmd rsh-enable
ip rcmd remote-host Rack1R1 10.1.1.1 Rack1R1 enable
!
no ip domain lookup
!
interface Loopback0
ip address 150.1.2.2 255.255.255.0
!
interface Ethernet0/0
ip address 10.1.1.2 255.255.255.0
!
router rip
version 2
network 10.0.0.0
network 150.1.0.0
no auto-summary
!
line con 0
line aux 0
line vty 0 4
password cisco
login
!
!
end
TCL Script:
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"
puts "**********************************"
puts "* Ping $hostname [lindex $int [expr $counter - 1]]"
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:
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.
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)#
本文出自 “沉沦-给生命另外一个起点” 博客,谢绝转载!