{"id":2866,"date":"2020-12-29T10:51:05","date_gmt":"2020-12-29T18:51:05","guid":{"rendered":"https:\/\/SUMMALAI.COM\/?p=2866"},"modified":"2020-12-29T10:55:18","modified_gmt":"2020-12-29T18:55:18","slug":"understand-bgp-regular-expressions-and-examples","status":"publish","type":"post","link":"https:\/\/SUMMALAI.COM\/?p=2866","title":{"rendered":"Understand BGP Regular Expressions and Examples"},"content":{"rendered":"\n<p>Regular Expressions are used often for BGP route manipulation or filtering. In this lesson,&nbsp;we\u2019ll take a look at some useful regular expressions. First, let\u2019s take a look at the different characters that we can use:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Characters<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>?<\/strong><\/td><td>repeats the previous character one or zero times.<\/td><\/tr><tr><td><strong>*<\/strong><\/td><td>repeats the previous character zero or many times.<\/td><\/tr><tr><td><strong>+<\/strong><\/td><td>repeats the previous character one or more times.<\/td><\/tr><tr><td><strong>^<\/strong><\/td><td>matches the beginning of a string.<\/td><\/tr><tr><td><strong>$<\/strong><\/td><td>matches the end of a string.<\/td><\/tr><tr><td><strong>[]<\/strong><\/td><td>is a range.<\/td><\/tr><tr><td><strong>_<\/strong><\/td><td>matches the space between AS numbers or the end of the AS PATH list.<\/td><\/tr><tr><td><strong>\\\\<\/strong><\/td><td>is an escape character. You\u2019ll need this for BGP confederations.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The dollar sign ($) regular expression character should be placed at the end of a Border Gateway Protocol (BGP) autonomous system (AS) path filter to indicate the originating AS. Regular expressions are used to locate character strings that match a particular pattern. AS path filters are used to permit or deny routes that match the regular expression.<\/p>\n\n\n\n<p>The $ character indicates that the preceding characters should match the end of the string. The originating router will insert its AS number into the AS path, and subsequent routers will prepend their AS numbers to the beginning of the AS path string. The last AS number in the AS path is the originating AS. For example, the ip as-path access-list 1 permit ^111_999$ command permits paths that originate from AS 999.<\/p>\n\n\n\n<p>The caret (^) character should be placed at the beginning of a BGP AS path filter to indicate the AS from which the path was learned. The ^ character indicates that the subsequent characters should match the start of the string. The first number in an AS path indicates the AS from which the path was learned. For example, the ip aspath accesslist 1 permit ^111_999$ command permits paths that are learned from AS 111.<\/p>\n\n\n\n<p>The underscore (_) character is used to indicate a comma, a brace, the start or end of an input string, or a space. When used between two AS path numbers, the _ character indicates that the ASes are directly connected. For example, the ip aspath accesslist 1 permit ^111_999$ command indicates that AS 111 and AS 999 should be directly connected.<\/p>\n\n\n\n<p>The period (.) character is used to represent any single character. For example, the ip aspath accesslist 1 permit ^&#8230;_999$ command permits paths that originate from AS 999 and are learned from any threedigit AS.<\/p>\n\n\n\n<p>The bracket (]) character is used to indicate a set of characters or a range of characters. For example, the ip aspath accesslist 1 permit ^[09]_999$ command permits paths that originate from AS 999 and are learned from any AS numbered from 0 through 9, and the ip aspath accesslist 1 permit ^[123]_999$ command permits paths that originate from AS 999 and are learned from AS 1, AS 2, or AS 3.<\/p>\n\n\n\n<p>The asterisk (*) character indicates zero or more sequences of the previous expression. For example, the expression [09]* indicates a string of zero or more digits. Therefore, the ip aspath accesslist 1 permit ^111_ [09]*$ command permits paths that are learned from AS 111 and originate from any AS.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Examples<\/h3>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><td><strong>^$<\/strong><\/td><td>matches an empty AS PATH so it will match all prefixes from the local AS.<\/td><\/tr><tr><td><strong>^51_<\/strong><\/td><td>matches prefixes from AS 51 that is directly connected to our AS.<\/td><\/tr><tr><td>&nbsp;<strong>_51_<\/strong><\/td><td>matches prefixes that transit AS 51.<\/td><\/tr><tr><td>&nbsp;<strong>_51$<\/strong><\/td><td>matches prefixes that originated in AS 51, the $ ensures that it\u2019s the beginning of the AS PATH.<\/td><\/tr><tr><td>&nbsp;<strong>^([0-9]+)_51<\/strong><\/td><td>matches prefixes from AS 51 where AS 51 is behind one of our directly connected AS\u2019es.<\/td><\/tr><tr><td>&nbsp;<strong>^51_([0-9]+)<\/strong><\/td><td>matches prefixes from the clients of directly connected AS 51.<\/td><\/tr><tr><td>&nbsp;<strong>^(51_)+([0-9]+)<\/strong><\/td><td>matches prefixes from the clients of directly connected AS 51, where AS 51 might be doing AS PATH prepending.<\/td><\/tr><tr><td>&nbsp;<strong>^51_([0-9]+_)+<\/strong><\/td><td>matches prefixes from the clients of directly connected AS 51, where the clients might be doing AS PATH prepending.<\/td><\/tr><tr><td><strong>^\\65200\\)<\/strong><\/td><td>matches prefixed from confederation peer 65200.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n<\/div>\n<\/div>\n\n\n\n<p>If you need some practice for these, I would suggest using a&nbsp;<a href=\"http:\/\/www.lookinglass.org\/\">BGP looking glass server<\/a>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using Regular Expressions in BGP<\/h3>\n\n\n\n<p>You can use regular expressions in the&nbsp;<strong><a href=\"https:\/\/www.cisco.com\/en\/US\/docs\/ios\/12_3\/iproute\/command\/reference\/ip2_i1g.html#wp1036594\" target=\"_blank\" rel=\"noreferrer noopener\">ip as-path access-list<\/a>&nbsp;<\/strong>command with Border Gateway Protocol (BGP). This document describes scenarios for using regular expressions. For more general information about regular expressions, see the Cisco Documentation on&nbsp;<a href=\"https:\/\/www.cisco.com\/en\/US\/docs\/ios\/12_2\/termserv\/configuration\/guide\/tcfaapre_ps1835_TSD_Products_Configuration_Guide_Chapter.html\" target=\"_blank\" rel=\"noreferrer noopener\">Regular Expressions<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Requirements<\/h3>\n\n\n\n<p>Readers of this document should be knowledgeable of the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Basic BGP configuration. For more information, see the&nbsp;<a href=\"https:\/\/www.cisco.com\/en\/US\/tech\/tk365\/technologies_tech_note09186a00800c95bb.shtml\" target=\"_blank\" rel=\"noreferrer noopener\">BGP Case Studies<\/a>&nbsp;and&nbsp;<a href=\"https:\/\/www.cisco.com\/en\/US\/docs\/ios\/12_2\/ip\/configuration\/guide\/1cfbgp.html\" target=\"_blank\" rel=\"noreferrer noopener\">Configuring BGP<\/a>.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Components Used<\/h3>\n\n\n\n<p>The information in this document is based on the software and hardware versions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Cisco IOS\u00ae Software Release 12.0<\/li><\/ul>\n\n\n\n<p>The information in this document was created from the devices in a specific lab environment. All of the devices used in this document started with a cleared (default) configuration. If your network is live, make sure that you understand the potential impact of any command.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conventions<\/h3>\n\n\n\n<p>For more information on document conventions, see the&nbsp;<a href=\"https:\/\/www.cisco.com\/en\/US\/tech\/tk801\/tk36\/technologies_tech_note09186a0080121ac5.shtml\" target=\"_blank\" rel=\"noreferrer noopener\">Cisco Technical Tips Conventions<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Network Scenarios<\/h3>\n\n\n\n<p>The following network diagram is referred to in these three scenarios.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.cisco.com\/c\/dam\/en\/us\/support\/docs\/ip\/border-gateway-protocol-bgp\/13754-26a.gif\"><img decoding=\"async\" src=\"https:\/\/www.cisco.com\/c\/dam\/en\/us\/support\/docs\/ip\/border-gateway-protocol-bgp\/13754-26a.gif\" alt=\"26a.gif\"\/><\/a><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Only Allow Networks Originating from AS 4 to Enter Router 1<\/h3>\n\n\n\n<p>If you would like for Router 1 to receive only the routes originated from AS 4 (and no Internet routes), you can apply an inbound access list on Router 1 as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ip as-path access-list 1 permit ^4$ \n\nrouter bgp 1 \n neighbor 4.4.4.4 remote-as 4 \n neighbor 4.4.4.4 route-map foo in \n\nroute-map foo permit 10 \n match as-path 1 <\/pre>\n\n\n\n<p>This ensures only networks originated from AS 4 are allowed into Router 1.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Only Allow Networks That Have Passed Through AS 4 to Enter AS 3<\/h3>\n\n\n\n<p>If you want only the networks that have passed through AS 4 to enter AS 3 from Router 3, you can apply an inbound filter on Router 3.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ip as-path access-list 1 permit _4_ \n\nrouter bgp 3 \n neighbor 2.2.2.2 remote-as 1 \n neighbor 2.2.2.2 route-map foo in \n\nroute-map foo permit 10 \n match as-path 1 <\/pre>\n\n\n\n<p>You can use an underscore (_) as the input string and output string in the<strong>&nbsp;<a href=\"https:\/\/www.cisco.com\/en\/US\/docs\/ios\/12_3\/iproute\/command\/reference\/ip2_i1g.html#wp1036594\" target=\"_blank\" rel=\"noreferrer noopener\">ip as-path access-list<\/a>&nbsp;<\/strong>command. Note that in this example anchoring (for instance, there is no ^) is not used, so it does not matter what autonomous systems come before and after AS 4.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Deny Networks Originated in AS 4 to Enter AS 3 and Permit all other Networks<\/h3>\n\n\n\n<p>If you want to deny all the networks that have originated in AS 4 and permit all other routes to enter AS 3 from Router 3, you can apply an inbound filter at Router 3, as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ip as-path access-list 1 deny _4$  \nip as-path access-list 1 permit .*\n\nrouter bgp 3 \n neighbor 2.2.2.2 remote-as 1 \n neighbor 2.2.2.2 route-map foo in \n\nroute-map foo permit 10 \n match as-path 1<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Only Allow Networks Originated from AS 4, and ASs Directly Attached to AS 4, to Enter Router 1<\/h3>\n\n\n\n<p>If you want AS 1 to get networks originated from AS 4 and all directly attached ASs of AS 4, apply the following inbound filter on Router 1.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ip as-path access-list 1 permit ^4_[0-9]*$ \n\nrouter bgp 1 \n neighbor 4.4.4.4 remote-as 4 \n neighbor 4.4.4.4 route-map foo in \n\nroute-map foo permit 10 \n match as-path 1 <\/pre>\n\n\n\n<p>In the&nbsp;<strong><a href=\"https:\/\/www.cisco.com\/en\/US\/docs\/ios\/12_3\/iproute\/command\/reference\/ip2_i1g.html#wp1036594\" target=\"_blank\" rel=\"noreferrer noopener\">ip as-path access-list<\/a>&nbsp;<\/strong>command, the carat (^) starts the input string and designates &#8220;AS&#8221;. The underscore (_) means there is a null string in the string that follows &#8220;AS 4&#8221;. The [0-9]* specifies that any connected AS with a valid AS number can pass the filter. The advantage of using the [0-9]* syntax is that it gives you the flexibility to add any number of ASs without modifying this command string. For additional information, see&nbsp;<a href=\"https:\/\/www.cisco.com\/en\/US\/tech\/tk365\/technologies_tech_note09186a00800c95bb.shtml#asregexp\" target=\"_blank\" rel=\"noreferrer noopener\">AS-Regular Expression<\/a>.<\/p>\n\n\n\n<p>Ref: https:\/\/www.cisco.com\/c\/en\/us\/support\/docs\/ip\/border-gateway-protocol-bgp\/13754-26.html<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Regular Expressions are used often for BGP route manipulation or filtering. In this lesson,&nbsp;we\u2019ll take a look at some useful regular expressions. First, let\u2019s 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. + <a class=\"read-more\" href=\"https:\/\/SUMMALAI.COM\/?p=2866\">Read More<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[400,4,7,9,19],"tags":[532,533],"class_list":["post-2866","post","type-post","status-publish","format-standard","hentry","category-ccnp","category-cert","category-network","category-networks","category-router-switch","tag-bgp","tag-bgp-regular-expressions"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/2866","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2866"}],"version-history":[{"count":3,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/2866\/revisions"}],"predecessor-version":[{"id":2869,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/2866\/revisions\/2869"}],"wp:attachment":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}