Cisco与Juniper设备上的正则表达式比较

本文对比了Cisco与Juniper设备上正则表达式的使用,通过具体的配置实例展示了在路由策略中如何设置正则表达式来控制AS路径。测试结果显示,两者的正则表达式语法存在差异,影响了路由学习。例如,Cisco中的"^(51)+_(61|62)?_89_.*"在Juniper中需改为"(51)+ (61|62)? 89 .*"。此外,还讨论了不同正则表达式在过滤特定AS路径时的写法差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先给出正则表达式的基本规则

使用三台Cisco2500实现图中的拓扑进行正则表达式测试:
由于Cisco配置大家都很熟悉,这里仅给出London用as-path prepend模拟路由的配置。
模拟as-path

access-list 1 permit 192.168.0.0 0.0.0.255
access-list 2 permit 192.168.2.0 0.0.0.255
access-list 3 permit 192.168.3.0 0.0.0.255
access-list 4 permit 192.168.4.0 0.0.0.255
access-list 5 permit 192.168.5.0 0.0.0.255
access-list 6 permit 192.168.6.0 0.0.0.255
access-list 7 permit 192.168.7.0 0.0.0.255
access-list 8 permit 192.168.8.0 0.0.0.255
access-list 9 permit 192.168.9.0 0.0.0.255

!
route-map set-path permit 10
 match ip address 1
 set as-path prepend 63 89 99
!
route-map set-path permit 20
 match ip address 2
 set as-path prepend 60
!
route-map set-path permit 30
 match ip address 3
 set as-path prepend 61
!
route-map set-path permit 40
 match ip address 4
 set as-path prepend 61 62
!
route-map set-path permit 50
 match ip address 5
 set as-path prepend 61 62 63
!
route-map set-path permit 60
 match ip address 6
 set as-path prepend 61 62 63 63
!
route-map set-path permit 70
 match ip address 7
 set as-path prepend 62 89 63
!
route-map set-path permit 80
 match ip address 8
 set as-path prepend 61 89 56 99
!
route-map set-path permit 90
 match ip address 9
 set as-path prepend 51 89
!
route-map set-path permit 100
!
router bgp 51
 neighbor 10.1.1.2 route-map set-path out


使用Juniper Olive跑三台logical-router实现图中的拓扑进行正则表达式测试:
root@ph# show
version 7.1R1.3;
system {
    host-name ph;
}
logical-routers {
    Denver {
        interfaces {
            fxp2 {
                unit 11 {
                    vlan-id 11;
                    family inet {
                        address 10.1.1.5/30;
                    }
                }
            }
        }
        protocols {
            bgp {
                group juniper {
                    type external;
                    peer-as 52;
                    neighbor 10.1.1.6;
                }
            }      
        }
        routing-options {
            autonomous-system 53;
        }
    }
    Florence {
        interfaces {
            fxp1 {
                unit 11 {
                    vlan-id 11;
                    family inet {
                        address 10.1.1.6/30;
                    }
                }
            }
            fxp2 {
                unit 10 {
                    vlan-id 10;
                    family inet {
                        address 10.1.1.2/30;
                    }
                }
            }      
        }
        protocols {
            bgp {
                group cisco {
                    type external;
                    peer-as 51;
                    neighbor 10.1.1.1;
                }
                group juniper {
                    type external;
                    peer-as 53;
                    neighbor 10.1.1.5 {
                        export test-filter;
                    }
                }
            }
        }
        policy-options {
            policy-statement test-filter {
                term term1 {
                    from as-path test;
                    then accept;
                }  
                term term2 {
                    then reject;
                }
            }
            as-path test "(51)+ (61|62)? 89 .*";
        }
        routing-options {
            autonomous-system 52;
        }
    }
    London {
        interfaces {
            fxp1 {
                unit 10 {
                    vlan-id 10;
                    family inet {
                        address 10.1.1.1/30;
                    }
                }
            }
        }
        protocols {
            bgp {  
                group cisco {
                    type external;
                    export redistribute-static;
                    peer-as 52;
                    neighbor 10.1.1.2;
                }
            }
        }
        policy-options {
            policy-statement redistribute-static {
                term term1 {
                    from {
                        protocol static;
                        route-filter 192.168.1.0/24 exact;
                    }
                    then accept;
                }
                term term2 {
                    from {
                        protocol static;
                        route-filter 192.168.2.0/24 exact;
                    }
                    then {
                        as-path-expand 60;
                        accept;
                    }
                }
                term term3 {
                    from {
                        protocol static;
                        route-filter 192.168.3.0/24 exact;
                    }
                    then {
                        as-path-expand 61;
                        accept;
                    }
                }
                term term4 {
                    from {
                        protocol static;
                        route-filter 192.168.4.0/24 exact;
                    }
                    then {
                        as-path-expand "61 62";
                        accept;
                    }
                }
                term term5 {
                    from {
                        protocol static;
                        route-filter 192.168.5.0/24 exact;
                    }
                    then {
                        as-path-expand "61 62 63";
                        accept;
                    }
                }
                term term6 {
                    from {
                        protocol static;
                        route-filter 192.168.6.0/24 exact;
                    }
                    then {
                        as-path-expand "61 62 63 63";
                        accept;
                    }
                }
                term term7 {
                    from {
                        protocol static;
                        route-filter 192.168.7.0/24 exact;
                    }
                    then {
                        as-path-expand "62 89 63";
                        accept;
                    }
                }
                term term8 {
                    from {
                        protocol static;
                        route-filter 192.168.8.0/24 exact;
                    }
                    then {
                        as-path-expand "61 89 56 99";
                        accept;
                    }
                }
                term term9 {
                    from {
                        protocol static;
                        route-filter 192.168.9.0/24 exact;
                    }
                    then {
                        as-path-expand "51 89";
                        accept;
                    }
                }
                term term0 {
                    from {
                        protocol static;
                        route-filter 192.168.0.0/24 exact;
                    }
                    then {
                        as-path-expand "63 89 99";
                        accept;
                    }
                }
            }
        }
        routing-options {
            static {
                route 192.168.1.0/24 reject;
                route 192.168.2.0/24 reject;
                route 192.168.3.0/24 reject;
                route 192.168.4.0/24 reject;
                route 192.168.5.0/24 reject;
                route 192.168.6.0/24 reject;
                route 192.168.7.0/24 reject;
                route 192.168.8.0/24 reject;
                route 192.168.9.0/24 reject;
                route 192.168.0.0/24 reject;
            }
            autonomous-system 51;
        }
    }
}
interfaces {
    fxp1 {
        vlan-tagging;
    }
    fxp2 {
        vlan-tagging;
    }
}

[edit]
root@ph#


Juniper Olive测试:
root@ph# run show route terse logical-router Florence

inet.0: 14 destinations, 14 routes (14 active, 0 holddown, 0 hidden)
+ = Active Route, - = Last Active, * = Both

A Destination        P Prf   Metric 1   Metric 2  Next hop        AS path
* 10.1.1.0/30        D   0                       >fxp2.10     
* 10.1.1.2/32        L   0                        Local
* 10.1.1.4/30        D   0                       >fxp1.11     
* 10.1.1.6/32        L   0                        Local
* 192.168.0.0/24     B 170        100            >10.1.1.1        51 63 89 99 I
* 192.168.1.0/24     B 170        100            >10.1.1.1        51 I
* 192.168.2.0/24     B 170        100            >10.1.1.1        51 60 I
* 192.168.3.0/24     B 170        100            >10.1.1.1        51 61 I
* 192.168.4.0/24     B 170        100            >10.1.1.1        51 61 62 I
* 192.168.5.0/24     B 170        100            >10.1.1.1        51 61 62 63 I
* 192.168.6.0/24     B 170        100            >10.1.1.1        51 61 62 63 63 I
* 192.168.7.0/24     B 170        100            >10.1.1.1        51 62 89 63 I
* 192.168.8.0/24     B 170        100            >10.1.1.1        51 61 89 56 99 I
* 192.168.9.0/24     B 170        100            >10.1.1.1        51 51 89 I

[edit logical-routers Florence policy-options]
root@ph# run show route terse logical-router Denver     

inet.0: 5 destinations, 5 routes (5 active, 0 holddown, 0 hidden)
+ = Active Route, - = Last Active, * = Both

A Destination        P Prf   Metric 1   Metric 2  Next hop        AS path
* 10.1.1.4/30        D   0                       >fxp2.11     
* 10.1.1.5/32        L   0                        Local
* 192.168.7.0/24     B 170        100            >10.1.1.6        52 51 62 89 63 I
* 192.168.8.0/24     B 170        100            >10.1.1.6        52 51 61 89 56 99 I
* 192.168.9.0/24     B 170        100            >10.1.1.6        52 51 51 89 I

后语:
1、"(51)+ (61|62)? 89 .*" 在Cisco路由器上写为:^(51)+_(61|62)?_89_.* ,不过在此次测试过程中,Cisco只能在Denver上学到192.168.7.0/24和192.168.8.0/24两条路由。
2、_6[^1-3]$在Cisco路由器上可以过滤出不以as61、62、63起源的路由,该正则表达式在Juniper路由器上必须写成".* [^61-63]",而不能写成".* 6[^1-3]"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值