The premiere source of truth powering network automation. Open and extensible, trusted by thousands.

NetBox is now available as a managed cloud solution! Stop worrying about your tooling and get back to building networks.

Extracting BGP info with regular expressions

By stretch | Saturday, May 10, 2008 at 11:54 a.m. UTC

A reader recently asked for some handy regular expressions for extracting useful information from BGP tables and outputs. Following are a few that I came up with. If you have any more please send them in or leave a comment!

To find all subnets originating from AS 100 (AS path ends with 100):

Router# show ip bgp regexp _100$
...
   Network          Next Hop            Metric LocPrf Weight Path
*  10.1.0.0/30      172.16.0.6                             0 300 100 ?
*>                  172.16.0.1               0             0 100 ?
*  172.16.0.0/30    172.16.0.6                             0 300 100 ?
*                   172.16.0.1               0             0 100 ?
...

To find all subnets reachable via AS 100 (AS path begins with 100):

Router# show ip bgp regexp ^100_
...
   Network          Next Hop            Metric LocPrf Weight Path
*> 10.1.0.0/30      172.16.0.1               0             0 100 ?
*  10.3.0.0/30      172.16.0.1                             0 100 300 ?
*  172.16.0.0/30    172.16.0.1               0             0 100 ?
*> 172.16.0.8/30    172.16.0.1               0             0 100 ?
...

To find all routes traversing AS 100:

Router# show ip bgp regexp _100_
...
   Network          Next Hop            Metric LocPrf Weight Path
*  10.1.0.0/30      172.16.0.6                             0 300 100 ?
*>                  172.16.0.1               0             0 100 ?
*  10.3.0.0/30      172.16.0.1                             0 100 300 ?
*  172.16.0.0/30    172.16.0.6                             0 300 100 ?
...

If you need to further filter the output, use quote-regexp instead. For example, to find all 172.x.x.x routes originating from AS 100:

Router# show ip bgp quote-regexp "_100$" | include ^.  172\.
*  172.16.0.0/30    172.16.0.6                             0 300 100 ?
*  172.31.0.1/32    172.16.0.6                             0 300 100 ?

Or, to find all subnets currently being reached via AS 100:

Router# show ip bgp quote-regexp "^100_" | i ^.> 
*> 10.1.0.0/30      172.16.0.1               0             0 100 ?
*> 172.16.0.8/30    172.16.0.1               0             0 100 ?
*> 172.31.0.1/32    172.16.0.1               0             0 100 ?

Unfortunately, Cisco's implementation of regular expressions is rather crippled. For (much) more advanced functionality, consider using Tcl scripting.

Posted in Tips and Tricks

Comments


Xboy_mr
May 10, 2011 at 5:33 p.m. UTC

جرمی، این ها دیگه چیه مینویسی؟ قبلا ها چیزای باحال تری می نوشتی. دمت گرم


ioio
May 6, 2012 at 12:38 p.m. UTC

excellent cheers


retsiger
August 31, 2012 at 11:51 a.m. UTC

thank you


techcisco
September 12, 2012 at 5:03 a.m. UTC

Hi Jeremy,

Please explain me how to find AS number two AS away from my AS.

ex: my AS is 17443, my next AS 47125, how to find next AS after that. Please explain me in regular expression.

Thank you.


Johnny.X
July 6, 2015 at 3:09 a.m. UTC

This article is greate! I was confused about the regexp and quote-regexp. Now its much more clear to me. Thank you!


A guest
April 19, 2016 at 6:06 p.m. UTC

great article


jojo
May 31, 2016 at 7:49 p.m. UTC

quote-regexp? You just blew my mind.

Comments have closed for this article due to its age.