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 A Series On The Same Topic

  1. Cisco IOS CLI Regex: sh ip bgp in (2nd May 2012)
  2. IOS CLI Tip: More accurate pipe commands (1st May 2012)
  3. Cisco Nexus NXOS and Fixing broken “switchto” syntax with alias (18th December 2011)
  4. show ip eigrp topology all (22nd May 2011)
  5. Cisco IOS CLI Shortcuts (6th February 2011)
  6. The poor man's IOS Traffic Generator (19th September 2009)
  7. IOS: "terminal monitor" on, off - logging to your terminal (17th September 2009)
  8. IOS: Console, Terminal, Monitor, VTY - what is what ? (16th September 2009)
  9. IOS: Clearing an interface configuration (13th September 2009)
  10. IOS: Setting Terminal Window Length (10th September 2009)
  11. IOS CLI: show run linenum (9th September 2009)
  12. IOS: Setting the TCP timeout on IOS (14th August 2008)
  13. IOS: enable and .... disable ? (20th July 2008)
  14. IOS: Reverse SSH console access - Part 2 (25th June 2008)
  15. IOS:Open Source Lab DNS and IP addressing (2nd June 2008)
  16. IOS: Reverse SSH console access (29th May 2008)
  17. ip tcp timestamp (14th April 2008)
  18. Cisco ASA and IOS command tip - test aaa-server (18th February 2008)
  • 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()