I often find myself searching for specific route in the routing table of an IOS device. In this case, I want to search for any routes that have the 10.4 in the IP address. So, instinctively I would use the extended syntax for the IOS CLI that uses the Unix pipe to grep the output. [^1]
SW2#sh ip route | i 10.4
O IA 172.19.5.0/24 [110/4] via 172.30.4.33, 7w0d, Vlan2493
O IA 172.19.4.0/24 [110/4] via 172.30.4.33, 7w0d, Vlan2493
O IA 172.30.4.248/29 [110/4] via 172.30.4.33, 4w1d, Vlan2493
O IA 172.30.2.23/32 [110/4] via 172.30.4.33, 7w0d, Vlan2493
O IA 172.30.2.22/32 [110/4] via 172.30.4.33, 4w1d, Vlan2493
O IA 172.30.2.21/32 [110/4] via 172.30.4.33, 4w1d, Vlan2493
O IA 172.30.2.6/32 [110/4] via 172.30.4.177, 7w0d, Vlan23
[110/4] via 172.30.4.33, 7w0d, Vlan2493
O IA 172.30.6.0/28 [110/4] via 172.30.4.33, 4w1d, Vlan2493
O 172.30.2.12/32 [110/4] via 172.30.4.177, 7w0d, Vlan23
[110/4] via 172.30.4.33, 7w0d, Vlan2493
O E2 10.4.20.0/24 [110/20] via 172.30.4.33, 5d06h, Vlan2493
O E2 10.4.18.0/24 [110/20] via 172.30.4.33, 5d06h, Vlan2493
O E2 10.4.56.6/32 [110/20] via 172.30.4.33, 5d06h, Vlan2493
O E2 10.244.12.160/28 [110/150] via 172.30.4.177, 10:45:06, Vlan23
[110/150] via 172.30.4.33, 10:45:06, Vlan2493
O E2 10.244.109.240/28 [110/150] via 172.30.4.177, 10:43:30, Vlan23
[110/150] via 172.30.4.33, 10:43:30, Vlan2493
O E2 10.244.110.48/28 [110/150] via 172.30.4.177, 5d06h, Vlan83
O E2 10.244.111.32/28 [110/150] via 172.30.4.177, 10:47:31, Vlan23
[110/150] via 172.30.4.33, 10:47:31, Vlan2493
O E2 10.244.10.48/28 [110/150] via 172.30.4.177, 4d04h, Vlan83
SW2#
As you can see, this isn’t giving me IP address that have ’10.4′ listed. Why ? That’s because a ‘.’ is REGEX syntax for any character. This ’10.4′ matches
|Match |Line|
| :-: | :-: |
| 10/4 | O IA 172.19.5.0/24 [110/4] via 172.30.4.33, 7w0d, Vlan2493 |
| 10.4 | O E2 10.4.56.6/32 [110/20] via 172.30.4.33, 5d06h, Vlan2493 |
And, although it’s not shown here, it would also any line that has other combinations of characters between 10 and 4.
You can make the dot a literal by putting a slash in front of it. So when I really want to match ONLY 10.4 I can do the following:
SW2#sh ip route | i 10\.4
O E2 10.4.20.0/24 [110/20] via 172.30.4.33, 5d06h, Vlan2493
O E2 10.4.18.0/24 [110/20] via 172.30.4.33, 5d06h, Vlan2493
O E2 10.4.56.6/32 [110/20] via 172.30.4.33, 5d06h, Vlan2493
O E2 10.244.110.48/28 [110/150] via 172.30.4.177, 5d06h, Vlan23
O E2 10.244.10.48/28 [110/150] via 172.30.4.177, 4d04h, Vlan23
SW2#
Note:
|Match |Line|
| :-: | :-: |
| 10.4 | O E2 10.244.10.48/28 [110/150] via 172.30.4.177, 4d04h, Vlan23 |
You could further improve this search by using a caret to indicate to the regex to only at the start of some text. But I’ll leave that to the read as an exercise.
[^1]: I’ll assume that you comprehend the idea of piping syntax for this article.
Other posts in the series
- Cisco IOS CLI Regex: sh ip bgp in
- IOS CLI Tip: More accurate pipe commands (This post)
- Cisco Nexus NXOS and Fixing broken “switchto” syntax with alias
- show ip eigrp topology all
- Cisco IOS CLI Shortcuts
- The poor man's IOS Traffic Generator
- IOS: "terminal monitor" on, off - logging to your terminal
- IOS: Console, Terminal, Monitor, VTY - what is what ?
- IOS: Clearing an interface configuration
- IOS: Setting Terminal Window Length
- IOS CLI: show run linenum
- IOS: Setting the TCP timeout on IOS
- IOS: enable and .... disable ?
- IOS: Reverse SSH console access - Part 2
- IOS:Open Source Lab DNS and IP addressing
- IOS: Reverse SSH console access
- ip tcp timestamp
- Cisco ASA and IOS command tip - test aaa-server


Pingback: Cisco IOS CLI Regex: sh ip bgp in — My EtherealMind