IOS CLI Tip: More Accurate Pipe Commands

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

  1. Cisco IOS CLI Regex: sh ip bgp in
  2. IOS CLI Tip: More accurate pipe commands (This post)
  3. Cisco Nexus NXOS and Fixing broken “switchto” syntax with alias
  4. show ip eigrp topology all
  5. Cisco IOS CLI Shortcuts
  6. The poor man's IOS Traffic Generator
  7. IOS: "terminal monitor" on, off - logging to your terminal
  8. IOS: Console, Terminal, Monitor, VTY - what is what ?
  9. IOS: Clearing an interface configuration
  10. IOS: Setting Terminal Window Length
  11. IOS CLI: show run linenum
  12. IOS: Setting the TCP timeout on IOS
  13. IOS: enable and .... disable ?
  14. IOS: Reverse SSH console access - Part 2
  15. IOS:Open Source Lab DNS and IP addressing
  16. IOS: Reverse SSH console access
  17. ip tcp timestamp
  18. Cisco ASA and IOS command tip - test aaa-server
About Greg Ferro

Greg Ferro is a Network Engineer/Architect, mostly focussed on Data Centre, Security Infrastructure, and recently Virtualization. He has over 20 years in IT, in wide range of employers working as a freelance consultant including Finance, Service Providers and Online Companies. He is CCIE#6920 and has a few ideas about the world, but not enough to really count.

He is a host on the Packet Pushers Podcast, blogger at EtherealMind.com and on Twitter @etherealmind and Google Plus

  • http://www.facebook.com/people/Andrey-Tyurin/100001268482260 Andrey Tyurin

    Very nice! Really!!

  • http://www.facebook.com/people/Andrey-Tyurin/100001268482260 Andrey Tyurin

     Very nice! Really!!

  • Andrew Jones

    Or when looking at a routing table, I use “show ip route 10.4.0.0 255.255.0.0 longer prefixes” and you get anything that’s a subnet of the 10.4/16 adress.

    Allthough, regex can be used for any command not just sh ip route.

  • Russell Heilling

    If IOS supported extended regular expression syntax the exercise for the reader could be done using | i b10.4. or | i <10.4. but it doesn't, so you would need to use | i 10.4. (that is backslash space, alternatively [ ])

    You can't use the caret(^) because that matches the beginning of a string, not the beginning of a word.

  • Erik Tamminga

    You can always pre-pend the 10.4 with a space, like in ” 10.4″ to exclude the remaining items. (although in this case a show ip route 10.4.0.0 255.255.0.0 would suffice, as already mentioned

  • Vitaliy Soldatov

    router#sh ip bgp | in (.*)( +)(.*)( +)0( +)100
    *>i10.10.11.0/24    10.11.11.1               0    100      0 i

  • Bryan Farmer

    I find the following useful on switches when I want to see the IP addresses without all the interfaces listed…

    show ip int brief | i .

    Another tip – it’s a bit quirky, but you can pipe after the include which functions somewhat like a logical or:

    show interface | i Ether|errors

    GigabitEthernet1/1 is up, line protocol is up (connected)
         0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
         0 output errors, 0 collisions, 1 interface resets
    GigabitEthernet1/2 is up, line protocol is up (connected)
         0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
         0 output errors, 0 collisions, 1 interface resets

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