1、CCNP BGP正则表达式BGP正则表达式在BGP的过滤路由机制里可以使用route-map,distribute-list,这些机制都需要我们定义好需要过滤的路由,如果现在需要过滤某一个AS的所有路由,而该AS又传递了大量的路由,那么使用以上工具来逐条定义路由再进行过滤的方法操作就十分繁杂,并且不便于管理。 BGP针对于上述情况设计了专门的解决方案:as-path access-list,专门针对于as-path来进行过滤。而匹配AS号的工具就是正则表达式。在计算机科学中,是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。R4#show ip bgp Network Nex
2、t Hop Metric LocPrf Weight Path* 1.1.1.1/32 34.1.1.3 0 300 200 100 i* 2.2.2.2/32 34.1.1.3 0 300 200 i* 3.3.3.3/32 34.1.1.3 0 0 300 i 表示一个表达式的开始,如果不用这个字符,那么默认就没有开始的限制了。R4(config)#ip as-path access-list 1 permit 300R4(config)#router bgp 400R4(config-router)#neighbor 34.1.1.3 filter-list 1 in表示只允许接收经过相
3、邻直连AS为300的路由,其余的全部过滤掉。正则表达式中的开始,只是查看bgp路由as-path列表的第一个AS,而不是真正经过AS路径的第一个AS(实际是相反的,as-path是从右往左算的)查看R4的bgp路由R4#show ip bgp Network Next Hop Metric LocPrf Weight Path* 1.1.1.1/32 34.1.1.3 0 300 200 100 i* 2.2.2.2/32 34.1.1.3 0 300 200 i* 3.3.3.3/32 34.1.1.3 0 0 300 i$ 表示一个表达式的结束,如果不用这个字符,那么默认就没有结束的限制了
4、。R4(config)#ip as-path access-list 1 permit 100$R4(config)#router bgp 400R4(config-router)#neighbor 34.1.1.3 filter-list 1 in表示允许接收起源AS为100的路由(根据上面的描述,正则表达式的开始与结束和as-path记录的开始与结束是相反的)查看R4的bgp路由R4#show ip bgp Network Next Hop Metric LocPrf Weight Path* 1.1.1.1/32 34.1.1.3 0 300 200 100 i? 表示匹配前面的一个字符
5、出现一次或者是空(零次)。Cisco路由器上使用trcl+v再?来输入问号R4(config)#ip as-path access-list 1 permit 10?$R4(config)#router bgp 400R4(config-router)#neighbor 34.1.1.3 filter-list 1 in表示允许接收起源AS为1或者10(10?代表前一个字符0出现零次,即为1,或者一次,即为10)的路由R1(config)#no router bgp 100R1(config)#router bgp 1R1(config-router)#network 1.1.1.1 mask
6、 255.255.255.255R1(config-router)#neighbor 12.1.1.2 remote-as 200查看R4的bgp路由R4#show ip bgp Network Next Hop Metric LocPrf Weight Path* 1.1.1.1/32 34.1.1.3 0 300 200 10 i将R1的AS改为10R1(config)#no router bgp 100R1(config)#router bgp 10R1(config-router)#network 1.1.1.1 mask 255.255.255.255R1(config-router
7、)#neighbor 12.1.1.2 remote-as 200查看R4的bgp路由R4#show ip bgp Network Next Hop Metric LocPrf Weight Path* 1.1.1.1/32 34.1.1.3 0 300 200 10 i* 表示匹配前面的一个字符出现多次或者是空,注意,和?不同的是?只允许匹配出现一次或者是空。R4(config)#ip as-path access-list 1 permit 10*$R4(config)#router bgp 400R4(config-router)#neighbor 34.1.1.3 filter-lis
8、t 1 in表示允许接收起源AS为1,10,100,1000,10000等路由现在将R1的AS改为1000R1(config)#router bgp 1000R1(config-router)#neighbor 12.1.1.2 remote-as 200R1(config-router)#network 1.1.1.1 mask 255.255.255.255查看R4的bgp路由R4#show ip bgp Network Next Hop Metric LocPrf Weight Path* 1.1.1.1/32 34.1.1.3 0 300 200 1000 i+ 表示匹配前面的一个字符
9、出现一次或者多次,但是不能是零次,而*可以出现零次。R4(config)#ip as-path access-list 1 permit 10*$R4(config)#router bgp 400R4(config-router)#neighbor 34.1.1.3 filter-list 1 in表示允许接收起源AS为10,100,1000,10000等路由. 表示任意单个字符,包括空格.* 就可以表示任意字符R4(config)#ip as-path access-list 1 deny 100$R4(config)#ip as-path access-list 1 permit .*R4
10、(config)#router bgp 400R4(config-router)#neighbor 34.1.1.3 filter-list 1 in表示接收除了起源AS为100以外的所有路由R4#show ip bgp Network Next Hop Metric LocPrf Weight Path* 2.2.2.2/32 34.1.1.3 0 300 200 i* 3.3.3.3/32 34.1.1.3 0 0 300 i_ 表示逗号,左大括号,右大括号,左小括号,右小括号R4(config)#ip as-path access-list 1 permit 300_200$R4(con
11、fig)#router bgp 400R4(config-router)#neighbor 34.1.1.3 filter-list 1 in表示只允许接收起源AS为200,直连相邻AS为300的路由R4#show ip bgp Network Next Hop Metric LocPrf Weight Path* 2.2.2.2/32 34.1.1.3 0 300 200 i300$ 表示只经过AS300,不包括其他任何ASR4(config)#ip as-path access-list 1 permit 300$R4(config)#router bgp 400R4(config-rou
12、ter)#neighbor 34.1.1.3 filter-list 1 inR4#show ip bgp Network Next Hop Metric LocPrf Weight Path* 3.3.3.3/32 34.1.1.3 0 0 300 i$ 表示本地始发的路由R3(config)#ip as-path access-list 1 deny $ R3(config)#ip as-path access-list 1 permit .*R3(config)#router bgp 300R3(config-router)#neighbor 34.1.1.4 filter-list 1
13、 out表示拒绝AS300本地始发的路由,允许其余所有路由R4#show ip bgp Network Next Hop Metric LocPrf Weight Path* 2.2.2.2/32 34.1.1.3 0 300 200 i* 3.3.3.3/32 34.1.1.3 0 0 300 iAa 表示匹配单个字符A或者aR4(config)#ip as-path access-list 1 permit 2300$R4(config)#router bgp 400R4(config-router)#neighbor 34.1.1.3 filter-list 1 in表示只允许接收起源A
14、S为200或300的路由R4#show ip bgp Network Next Hop Metric LocPrf Weight Path* 2.2.2.2/32 34.1.1.3 0 300 200 i* 3.3.3.3/32 34.1.1.3 0 0 300 i1-36-9 表示匹配单字符1、2、3、6、7、8、9中的任意一个R4(config)#ip as-path access-list 1 permit 1-300$R4(config)#router bgp 400R4(config-router)#neighbor 34.1.1.3 filter-list 1 in表示允许接收起源
15、AS为100或200或300的路由R4#show ip bgp Network Next Hop Metric LocPrf Weight Path* 1.1.1.1/32 34.1.1.3 0 300 200 100 i* 2.2.2.2/32 34.1.1.3 0 300 200 i* 3.3.3.3/32 34.1.1.3 0 0 300 i1-3 中括号中的符号表示反转字符的意思R4(config)#ip as-path access-list 1 permit 04-900R4(config)#router bgp 400R4(config-router)#neighbor 34.1
16、.1.3 filter-list 1 in表示允许接收经过除了AS0,AS400,AS500,AS600,AS700,AS800,AS900以外的所有路由R4#show ip bgp Network Next Hop Metric LocPrf Weight Path* 1.1.1.1/32 34.1.1.3 0 300 200 100 i* 2.2.2.2/32 34.1.1.3 0 300 200 i* 3.3.3.3/32 34.1.1.3 0 0 300 i1 2 表示使用as-path 选项聚合路由,从as 1和as 2来的路由形成聚合(65530) 表示与对等体形式联盟,该对等体在
17、as 655301 254_0-9*_253$ 表示起源于AS253,直连相邻AS为254,中间有一个任意AS2 254_0-9_*_253$ 表示起源于AS253,直连相邻AS为254,中间包括多个任意AS3 254_0-9?_253$ 表示起源于AS253,直连相邻AS为254,中间包括一个AS,AS0-AS9或中间没有AS4 254_0-9+_253$ 表示起源于AS253,直连相邻AS为254,相对于1来说,其中间必须存在一个AS,而1可以只有AS254,AS2535 254_0-9_+253$ 表示起源于AS253,直连相邻AS为254,相对于2来说,其中间必须还经过一个AS或多个A
18、S,而2可以只经过AS254,AS253BGP的前缀告警门限作用就是当路由器接收到了设置的前缀上限的时候,那么就应该停止接收前缀,并且发出告警等一系列的动作,是让邻居down还是其他动作。R1(config-router)#neighbor 12.1.1.2 maximum-prefix 1000 75 restart 1表示接收邻居12.1.1.2的路由条目超过了1000*75%=750条,即751条时发送告警消息,如果超过了1000条,即1001条则在1分钟后重置bgp连接。R1(config-router)#neighbor 12.1.1.2 maximum-prefix 1000 75 warning-only表示即使接收邻居12.1.1.2的路由条目超过了1000条,也只发送告警消息。