取消
显示结果 
搜索替代 
您的意思是: 
cancel
公告

December 2020

Cisco IOS 宏命令

5577
查看次数
30
有帮助
10
评论
本帖最后由 blakegao 于 2015-6-16 23:32 编辑
众所周知,在网络工程师的日常工作中,可能需要管理的网络设备(交换机、路由器、防火墙、AP等)会上百台甚至上千台,这个时候如何有效率地进行管控和处理日常事务就显得非常必要了。批处理和宏是一个比较普遍的方法,能为网络工程师的日常工作提高极大效率,而Cisco IOS中也包含了宏命令,能对设备进行批量处理。

路由器上的宏命令在思科路由器中可以使用TCL脚本语言来编辑脚本实现宏管控的目的,同时高端型号的交换机(6509系列等)也能提供TCL脚本环境。

Router#tclsh 进入TCL脚本编译环境
Router(tcl)tclquit 退出TCL脚本编译环境
TCL脚本编译之后不会出现在配置文件中。


首先在记事本编辑TCL脚本语言:
foreach TEST {
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5
192.168.1.6
192.168.1.7
192.168.1.8
192.168.1.9
192.168.1.10
} { puts [exec "ping $TEST"] }
能通过循环语句一次Ping上面的IP地址,直到最后一个IP地址。
然后在路由器中运行:

Router>enable
Router#tclsh
Router(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

这样就可以通过一条命令来简单测试全网的连通性了,而不需要繁琐地一个一个PING命令打!
交换机上的宏命令交换机IOS上同样存在宏命令,不过略显不同。交换机使用macro命令来实现定义宏命令,该宏命令可以保存到配置文件中。
定义交换机宏命令:

macro name TEST
doping 192.168.255.1
doping 192.168.255.2
doping 192.168.255.3
doping 192.168.255.4
doping 192.168.255.5
doping 192.168.255.6
doping 192.168.255.7
doping 192.168.255.8
doping 192.168.255.9
doping 192.168.255.10
@

实施调用:
Switch(config)#macro global apply TEST
即可以实施逐个ping以上地址的效果了。
另外思科交换机中默认已经定义了一些macro宏命令,例如cisco-desktop. cisco-phone. cisco-switch. cisco-router等。
评论
Fusen Huang
Cisco Employee
谢谢楼主分享!
lelwu
Cisco Employee
linlinwa
Cisco Employee
给力
linchen2
Community Member
赞赞赞
one-time
Expert
顶贴!
xupeng
Cisco Employee
太棒了,谢谢楼主分享
lianl
Cisco Employee
32个赞!
Fisheryu
Cisco Employee
:P:P:P
sxsure001
Beginner
lol:lol赞赞
linchen2
Community Member
赞啊
创建
认可您的同行
Content for Community-Ad