BGP Regular Expressions Examples

Lesson Contents

Regular Expressions are used often for BGP route manipulation or filtering. In this lesson we’ll take a look at some useful regular expressions. First let’s take a look at the different characters that we can use:

Characters

?
repeats the previous character one or zero times.
*
repeats the previous character zero or many times.
+
repeats the previous character one or more times.
^
matches the beginning of a string.
$
matches the end of a string.
[]
is a range.
_
matches the space between AS numbers or the end of the AS PATH list.
\\
is an escape character. You’ll need this for BGP confederations.

Examples

^$ matches an empty AS PATH so it will match all prefixes from the local AS.
^51_ matches prefixes from AS 51 that is directly connected to our AS.
 _51_ matches prefixes that transit AS 51.
 _51$ matches prefixes that originated in AS 51, the $ ensures that it’s the beginning of the AS PATH.
 ^([0-9]+)_51 matches prefixes from AS 51 where AS 51 is behind one of our directly connected AS’es.
 ^51_([0-9]+) matches prefixes from the clients of directly connected AS 51.
 ^(51_)+([0-9]+) matches prefixes from the clients of directly connected AS 51, where AS 51 might be doing AS PATH prepending.
 ^51_([0-9]+_)+ matches prefixes from the clients of directly connected AS 51, where the clients might be doing AS PATH prepending.
^\65200\) matches prefixed from confederation peer 65200.

If you need some practice for these, I would suggest to use a BGP looking glass server.

Got some more useful BGP regular expressions? Please let me know!

Tags:


Forum Replies

  1. Is it abnormal not really to be real fluent with these?

    Don’t get me wrong I see the examples and as I went through lessons later on I see some of the examples come up that can be useful.

    I am good with ones like:

    ^$ which can be useful for applying to everything (you use this one when dealing with Transit issues when multi-homing and need to filter) or ^63100$ apply to an AS specifically.

    I also get and like the ones like

    //cdn-forum.networklessons.com/uploads/default/original/1X/ba8410b24c63f01693bd194e49f4b85b377d378c.PNG

    which would give every AS that

    ... Continue reading in our forum

  2. Hi Brian,

    It’s normal I think…as network engineers, we don’t use regular expressions much. If you are into programming, you’ll use them quite a lot to match strings/numbers/etc… If you want to practice these, try a site like:

    https://regexr.com/

    Paste in the output of a BGP table there and test it…it’s easier and quicker than testing regex on your router.

    I wouldn’t worry about this too much though…when you need to use them for BGP, you can always look them up…test it, then apply it to your router. No need to memorize all the different options. When you need it

    ... Continue reading in our forum

  3. Hi Nitay,

    Sometimes, there are multiple ways to achieve the same thing with regex. The _ matches the space in between the AS numbers.

    If this is what you want, I would use this regex:

    show ip bgp regex ^([0-9
    ... Continue reading in our forum

  4. Hello Gowthamraj

    Looking at the various regular expressions can become confusing if you don’t actually use them. The ideal way to learn is to practice, and see how they behave as you apply them. The following lesson uses an example of a regular expression to apply a filter list to prevent a BGP transit AS situation:

    https://networklessons.com/bgp/bgp-prevent-transit-as


    You can also take a look at actual examples at the following Cisco documentation:

    https://www.cisco.com/c/en/us/support/docs/ip/border-gateway-protocol-bgp/13754-26.html


    Finally, you can use

    ... Continue reading in our forum

  5. Thank you Lagapides i will look into these

6 more replies! Ask a question or join the discussion by visiting our Community Forum