View Only
last person joined: 3 days ago 

Ask questions and share experiences about ACX Series, CTP Series, MX Series, PTX Series, SSR Series, JRR Series, and all things routing, including portfolios and protocols.
  • 1.  BGP AS REGX practice

    Posted 02-24-2024 21:27
    Edited by LEEBAHI 02-24-2024 23:07

    Hi everyone,

    I  am trying to learn how  BGP AS path REGX can be used to manipulate BGP attributes. I have set up my mock set up as show below:

    Above we have:

    ISP AS 2 sending  with AS path 11 12 13 14 155 2 towards AS1.  On MX1  we receive  this route . We  then apply BGP REGX to search AS path with certain criteria  and set local-pref 200  . We then apply this policy as export  towards  IBGP peer MX2 .  MX1 has been configured to set next hop to its peering IP ( so MX2 will accept the route.

    My REGX command does not take effect:


    MX1 does receive BGP route with AS 11 12 13 14 15 155 2 as shown above.

    We then apply the following policy as export  to IBGP group (MX2)

    set policy-options policy-statement TEST from as-path BGP-REGX
    set policy-options policy-statement TEST then  local-preference 200

    set policy-options policy-statement TEST then next-hop self

    set policy-options policy-statement TEST then accept

    set policy-options as-path BGP-REGX 2$

    The policy say if there is numeral 2 at the very end of AS path,  the local pref should be set to 200, so should have local pref set to 200 before it is announced to MX2.

    I verified  BGP REGX syntax is correct:

    I then applied the policy as export to iBGP group ( MX2) and clear the   BGP session  :


    set protocols bgp group INT export TEST
    set protocols bgp group INT neighbor

    Nothing advertised!

    If I removed:

    delete  policy-options policy-statement TEST from as-path BGP-REGX is advertised with local pref 200.

    So clearly BGP REGX  statement is a issue

     Syntax " $2 " bgp regex is correct as verified on "" link shown above.

    What am i missing?

    Thanks !!

    Be kind!!

  • 2.  RE: BGP AS REGX practice
    Best Answer

    Posted 02-25-2024 01:39


    Technically, your regex "2$" should match every route, which is originated from AS 2. However, because there is no "space" in front of 2, Junos interprets it as literally AS path of 2 only, without anything in front of it. So, if the AS path contained ONLY ASN 2, it would match.

    Therefore, try using following regex instead of currently used - ".* 2$". In this case, it will match any AS path which contains ASN 2 at the end.

    Also, I would advise to test regex, which you are intending to use, on the routing table before you use it in policy. In your case, if you check the routing table with the command like show route as-path-regex "2$", it will not produce output, while running show route as-path-regex ".* 2$", you will see needed route.


  • 3.  RE: BGP AS REGX practice

    Posted 02-25-2024 16:05

    Thanks Farid.

    it works. But I have been following juniper documentation that shows we can use   underscore to represent white space in AS path.

    For example:

    AS  11 12 13 14 15 155 2

    How do we define AS path regex to filter  AS path based on transit AS say AS 13 in my example.   I tried _13_    but it did not work. 

    Much appreciated!!

    Be kind!!

  • 4.  RE: BGP AS REGX practice

    Posted 02-25-2024 18:04
    Edited by LEEBAHI 02-25-2024 18:04

    I think I got it.

    it will be    .*13*.

    Much appreciated!!

    Be kind!!

  • 5.  RE: BGP AS REGX practice

    Posted 02-26-2024 01:45

    I would say ".* 13 .*"

    Check this link, really useful with examples -

    So, for space between ASNs, you don't need to use "_",  just space is sufficient.


  • 6.  RE: BGP AS REGX practice

    Posted 02-26-2024 06:01

    As others already replied, your regex should actually be: ".* 2" or ".* 2$".

    Junos regular expressions always implicitly prepend '^' at the beginning and append '$' at the end, unless they already exist. So, "xxx" actually means "^xxx$".

    This behavior differs significantly from e.g. Cisco and Huawei, which prepend and append '_', so as-path regex "2" in their OSs is actually interpreted as "^.* 2 .*$" (AS2 being a part of the AS_PATH in general, no matter where its position in the AS_PATH is).

    Same applies for community regular expressions.

    Berislav Todorovic

  • 7.  RE: BGP AS REGX practice

    Posted 02-27-2024 05:34

    Interesting point, never knew about that implicit prepend.

    Now behavior of Junos and IOS seems reasonable.