BGP Regular Expressions Examples

Lesson Contents

Regular Expressions (regex for short) are often used for BGP route manipulation or filtering. In this lesson, we’ll 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 using a BGP looking glass server.

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

Tags:


Forum Replies

  1. Hey Rene,

    I have found one of Swisscom BGP router from that expressions:

    AS path access list 1
        permit ^$
        permit 650[0-9][0-9][)]$
    

    permit ^$ : I think this one is written for locally originating routes

    I can’t get a meaning the last one’s " [)] "section ? You have an idea?
    By the way ; these ip as-path access lists , bgp filtering commands just filtering the AS’s that advertising from another BGP routers right?

    Deniz

  2. Hi Deniz,

    The first entry will match on prefixes that originated in this AS. The second one will match on everything that ends with 650XX). Anything in the 64512 – 65535 range are private AS numbers. The [)] is a bit strange, normally you use the [] for a range (like 0-9). I’m guessing that they use it to match on sub-AS numbers in a confederation? That’s the only time you will see a ) in the AS path:

    https://networklessons.com/bgp/bgp-confederation-explained/

    Rene

  3. Hi Rene,
    Just looked at BGP looking glass server. How can one practice regular expression on this site? Thanks!
    A.

  4. Hi A,

    Most of the looking glass servers support regular expressions so that would be the best option to get some practice. If you want to practice this “locally” then I would configure some BGP routers and use route-maps for things like AS path prepending, this can be used as a nice simulation of the Internet.

    Rene

  5. 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

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