众所周知,在网络工程师的日常工作中,可能需要管理的网络设备(交换机、路由器、防火墙、AP等)会上百台甚至上千台,这个时候如何有效率地进行管控和处理日常事务就显得非常必要了。批处理和宏是一个比较普遍的方法,能为网络工程师的日常工作提高极大效率,而Cisco IOS中也包含了宏命令,能对设备进行批量处理。
路由器上的宏命令
在思科路由器中可以使用TCL脚本语言来编辑脚本实现宏管控的目的,同时高端型号的交换机(6509系列等)也能提供TCL脚本环境。
12Router#tclsh 进入TCL脚本编译环境Router(tcl)tclquit 退出TCL脚本编译环境
TCL脚本编译之后不会出现在配置文件中。
首先在记事本编辑TCL脚本语言:
123456789101112foreach TEST {192.168.1.1192.168.1.2192.168.1.3192.168.1.4192.168.1.5192.168.1.6192.168.1.7192.168.1.8192.168.1.9192.168.1.10} { puts [exec"ping $TEST"] }
能通过循环语句一次Ping上面的IP地址,直到最后一个IP地址。
然后在路由器中运行:
123Router>enableRouter#tclshRouter(tcl)# 粘贴上述TCL脚本语言
Type escape sequence to abort.
Sending 5, 100‐byte ICMP Echos to 192.168.1.1, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round‐trip min/avg/max = 1/2/4 ms
Type escape sequence to abort.
Sending 5, 100‐byte ICMP Echos to 192.168.1.2, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round‐trip min/avg/max = 56/58/60 ms
Type escape sequence to abort.
Sending 5, 100‐byte ICMP Echos to 192.168.1.3, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round‐trip min/avg/max = 84/86/89 ms
Type escape sequence to abort.
Sending 5, 100‐byte ICMP Echos to 192.168.1.4, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round‐trip min/avg/max = 140/147/164 ms
Type escape sequence to abort.
Sending 5, 100‐byte ICMP Echos to 192.168.1.5, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round‐trip min/avg/max = 140/142/144 ms
Type escape sequence to abort.
Sending 5, 100‐byte ICMP Echos to 192.168.1.6, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round‐trip min/avg/max = 56/59/65 ms
Type escape sequence to abort.
Sending 5, 100‐byte ICMP Echos to 192.168.1.7, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round‐trip min/avg/max = 1/2/4 ms
Type escape sequence to abort.
Sending 5, 100‐byte ICMP Echos to 192.168.1.8, timeout is 2 seconds:
…..
Success rate is 0 percent (0/5)
Type escape sequence to abort.
Sending 5, 100‐byte ICMP Echos to 192.168.1.9, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round‐trip min/avg/max = 1/2/4 ms
Type escape sequence to abort.
Sending 5, 100‐byte ICMP Echos to 192.168.1.10, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round‐trip min/avg/max = 56/57/60 ms
Sending 5, 100‐byte ICMP Echos to 192.168.1.1, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round‐trip min/avg/max = 1/2/4 ms
Type escape sequence to abort.
Sending 5, 100‐byte ICMP Echos to 192.168.1.2, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round‐trip min/avg/max = 56/58/60 ms
Type escape sequence to abort.
Sending 5, 100‐byte ICMP Echos to 192.168.1.3, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round‐trip min/avg/max = 84/86/89 ms
Type escape sequence to abort.
Sending 5, 100‐byte ICMP Echos to 192.168.1.4, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round‐trip min/avg/max = 140/147/164 ms
Type escape sequence to abort.
Sending 5, 100‐byte ICMP Echos to 192.168.1.5, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round‐trip min/avg/max = 140/142/144 ms
Type escape sequence to abort.
Sending 5, 100‐byte ICMP Echos to 192.168.1.6, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round‐trip min/avg/max = 56/59/65 ms
Type escape sequence to abort.
Sending 5, 100‐byte ICMP Echos to 192.168.1.7, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round‐trip min/avg/max = 1/2/4 ms
Type escape sequence to abort.
Sending 5, 100‐byte ICMP Echos to 192.168.1.8, timeout is 2 seconds:
…..
Success rate is 0 percent (0/5)
Type escape sequence to abort.
Sending 5, 100‐byte ICMP Echos to 192.168.1.9, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round‐trip min/avg/max = 1/2/4 ms
Type escape sequence to abort.
Sending 5, 100‐byte ICMP Echos to 192.168.1.10, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round‐trip min/avg/max = 56/57/60 ms
这样就可以通过一条命令来简单测试全网的连通性了,而不需要繁琐地一个一个PING命令打!
交换机上的宏命令
交换机IOS上同样存在宏命令,不过略显不同。交换机使用macro命令来实现定义宏命令,该宏命令可以保存到配置文件中。
定义交换机宏命令:
123456789101112macro name TESTdoping192.168.255.1doping192.168.255.2doping192.168.255.3doping192.168.255.4doping192.168.255.5doping192.168.255.6doping192.168.255.7doping192.168.255.8doping192.168.255.9doping192.168.255.10@
实施调用:
1Switch(config)#macro global apply TEST
即可以实施逐个ping以上地址的效果了。
另外思科交换机中默认已经定义了一些macro宏命令,例如cisco-desktop. cisco-phone. cisco-switch. cisco-router等。
本文介绍在网络工程师日常工作中,如何利用批处理和宏命令提高工作效率。包括使用TCL脚本语言在路由器上批量测试网络连通性,以及在交换机上定义宏命令实现类似功能。
1124

被折叠的 条评论
为什么被折叠?



