首先說一下路由器命令輸出過濾的語法:任意show命令+管道符|+過濾器
這裏的過濾器包括以下三種:
include和exclude:顯示包含或者不包含特定正則表達式的行
begin:從符合特定正則表達式的行開始顯示
section:僅顯示特定符合正則表達式的section(所謂的section就是從一個非空格打頭的行開始,直到下一個非空格打頭的行之前結束,常用的是路由協議配置命令部分)
對於more命令的輸出僅支持include,exclude和begin,其他的命令都不支持命令輸出的過濾,這點和UNIX的針對任意命令都可以使用管道符是不同的,同時也不能像UNIX那樣可以對過濾進行級聯。
下面先以一個簡單的例子來介紹一下輸出的過濾。
show interface命令可以給我們很多信息的輸出,比如:
R1#show interfaces
FastEthernet0/0 is up, line protocol is up
Hardware is DEC21140, address is ca00.0f08.0000 (bia ca00.0f08.0000)
Internet address is 137.1.1.1/27
MTU 1500 bytes, BW 100000 Kbit, DLY 100 usec,
reliability 255/255, txload 1/255, rxload 1/255
Encapsulation ARPA, loopback not set
Keepalive set (10 sec)
Half-duplex, 100Mb/s, 100BaseTX/FX
ARP type: ARPA, ARP Timeout 04:00:00
Last input never, output 00:00:09, output hang never
Last clearing of "show interface" counters never
Input queue: 0/75/0/0 (size/max/drops/flushes); Total output drops: 0
Queueing strategy: fifo
Output queue :0/40 (size/max)
5 minute input rate 0 bits/sec, 0 packets/sec
5 minute output rate 0 bits/sec, 0 packets/sec
.....
其實我們可能僅僅關注某部分的信息,比如通常會關心所有的端口是不是工作在全雙工的模式下,也就是Half-duplex, 100Mb/s, 100BaseTX/FX這部分的內容,那麼我們加個過濾條件看一下:
R1#show interfaces | include duplex
Half-duplex, 100Mb/s, 100BaseTX/FX
Half-duplex, 100Mb/s, 100BaseTX/FX
這樣簡潔多了吧,可是不夠完美,發現了半雙工,但是不知道是哪個端口怎麼辦,我們在擴展下這個輸出過濾:
R1#show interfaces | include duplex|Ethernet
FastEthernet0/0 is up, line protocol is up
Half-duplex, 100Mb/s, 100BaseTX/FX
FastEthernet3/0 is administratively down, line protocol is down
Half-duplex, 100Mb/s, 100BaseTX/FX
恩,這下比較完美了。這裏出現了兩個管道符|,後面這個|不是對前面結果的再過濾,而是正則裏面or的功能。
每次輸入這麼多是不是不爽,那就定義一個別名來簡化吧:
R1#conf t
Enter configuration commands, one per line. End with CNTL/Z.
R1(config)#alias exec duplex show interfaces | include duplex
R1(config)#end
R1#duplex
Half-duplex, 100Mb/s, 100BaseTX/FX
Half-duplex, 100Mb/s, 100BaseTX/FX
然後再看一個section的例子,有時我們會只關注路由器上關於路由協議的配置,一般都會用下面的命令show run | begin router
這樣顯示的不但包含路由配置還會有以後更多的配置,這時候就可以使用section過濾(注意不是所有的IOS版本都支持section)
router1#show run | section router
hostname router1
router ospf 100
log-adjacency-changes
network 0.0.0.0 255.255.255.255 area 0
唉,還是不夠完美,把配置路由器名字的命令也顯示出來了,只好再修改一下了:
router1#sh run | section ^router
router ospf 100
log-adjacency-changes
network 0.0.0.0 255.255.255.255 area 0
這下就比較完美了。^router 就是代表以router打頭的行了,這樣就把hostname router1給排出再外了。
最後再來一個比較複雜點的輸出過濾。在OSPF中show ip ospf neighbor不能夠顯示出鄰居的area,只能通過show ip ospf neighbor detail命令,
Neoshi#show ip ospf neighbor detail
Neighbor 172.16.0.21, interface address 172.16.1.2
In the area 0 via interface Serial0/0/0.100
Neighbor priority is 0, State is FULL, 6 state changes
DR is 0.0.0.0 BDR is 0.0.0.0
Options is 0x52
LLS Options is 0x1 (LR)
Dead timer due in 00:00:37
Neighbor is up for 00:39:02
Index 1/1, retransmission queue length 0, number of retransmission 1
First 0x0(0)/0x0(0) Next 0x0(0)/0x0(0)
Last retransmission scan length is 1, maximum is 1
Last retransmission scan time is 0 msec, maximum is 0 msec
輸出信息很多,而實際上最有用的就是前三行的輸出。
Neoshi#show ip ospf neighbor detail | include (Neighbor.*(interface|priority))|area
Neighbor 172.16.0.21, interface address 172.16.1.2
In the area 0 via interface Serial0/0/0.100
Neighbor priority is 0, State is FULL, 6 state changes
Neighbor 172.16.0.12, interface address 10.0.0.6
In the area 0 via interface FastEthernet0/0
Neighbor priority is 1, State is FULL, 6 state changes
這個正則稍微複雜點,不過輸出還是達到了預定目標。
上面只是對命令輸出過濾的一些總結,除了過濾器的選擇外最重要的就是正則表達式的編寫了,關於正則有專門的書我就不多說了。其實更進一步還可以對輸出進行重新的排版,從而使輸出變的更簡潔,更有效,不過這就要用到TCL腳本。下面是一個學別人配置的別名爲ipconfig的腳本輸出示例,有興趣的大家可以研究一下。
R1#ifconfig
Interface IP Address Mask MTU State
=================================================================
FastEthernet0/0 172.18.25.1 255.255.255.0 1500 up
FastEthernet0/1 no address admin down
Serial0/0/0 no address up
Serial0/0/0.101 192.168.201.2 255.255.255.252 1500 up
Serial0/1/0 no address up/down