r1:
inter f0/0
ip add 192.168.1.1 255.255.255.0
no sh
inter e1/0
ip add 192.168.6.2 255.255.255.0
no sh
router bgp 1 //启用bgp路由,as为1
nei 192.168.1.2 remoet-as 1 //定义ibgp邻居
nei 192.168.6.2 remote-as 1 //定义ibgp邻居
net 192.168.1.0 //通告路由到bgp
net 192.168.6.0 //通告路由到bgp
r2:
inter e1/0
ip add 192.168.1.2 255.255.255.0
no sh
inter f0/0
ip add 192.168.2.1 255.255.255.0
no sh
router bgp 1 //启用bgp路由,as为1
nei 192.168.1.1 remote-as 1 //定义ibgp邻居
nei 192.168.2.2 remote-as 2 //定义ebgp邻居
nei 192.168.2.2 route-map set-med out //引用名为set-med的route-map,作用为当通告给邻居路由的时候改变metric
nei 192.168.2.2 route-map set-lo in //引用名为set-lo的route-map,作用为当邻居通告路由进入as1时改变本地优先级
net 192.168.1.0 //通告路由到bgp
net 192.168.2.0 //通告路由到bgp
access-list 1 permit 192.168.1.0 //这是改变metric引用的acl
access-list 2 permit 192.168.2.0 //这是改变metric引用的acl
access-list 3 permit 172.16.1.0 //这是改变本地优先级引用的acl
route-map set-med permit 10 //改变metric的route-map,序列号为10
match ip add 1 //同acl1匹配
set metric 100 //如果匹配就将metric改为100
ex
route-map set-med permit 20 //改变metric的route-map,序列号为20
match ip add 2 //同acl2匹配
set metric 200 //如果匹配就将metric改为200
ex
route-map set-lo permit 10 //改变本地优先级的route-map,序列号为10
match ip add 3 //引用acl3
set local-preference 100 //如果匹配就将本地优先级改为100
route-map set-lo permit 20 //没有match的route-map同所有的条目匹配
ex
end
clear ip bgp 192.168.2.2 soft //软重置邻居192.168.2.2,注意这条命令是在特权模式下使用的
r3:
inter e1/0
ip add 192.168.6.1 255.255.255.0 //r3的配置同r2差不多,不再多说
no sh
inter f0/0
ip add 192.168.5.2 255.255.255.0
ex
router bgp 1
nei 192.168.6.2 remote-as 1
nei 192.168.5.1 remote-as 2
net 192.168.6.0
net 192.168.5.0
nei 192.168.5.1 route-map set-med out
nei 192.168.5.1 route-map set-lo in
ex
access-list 1 permit 192.168.1.0
access-list 2 permit 192.168.6.0
access-list 3 permit 172.16.1.0
route-map set-med permit 10
match ip add 1
set metric 200
ex
route-map set-med permit 20
match ip add 2
set metric 100
route-map set-lo permit 10
match ip add 3
set local-preference 200
ex
route-map set-lo permit 20
ex
end
clear ip bgp 192.168.5.1 soft
r4:
inter e1/0
ip add 192.168.2.2 255.255.255.0
no sh
inter f0/0
ip add 192.168.3.1 255.255.255.0
no sh
router bgp 2
nei 192.168.2.1 remote-as 1
nei 192.168.3.2 remote-as 2
net 192.168.2.0
net 192.168.3.0
r5:
inter e1/0
ip add 192.168.5.1 255.255.255.0
no sh
inter f0/0
ip add 192.168.4.2 255.255.255.0
no sh
router bgp 2
nei 192.168.5.2 remote-as 1
nei 192.168.4.1 remote-as 2
net 192.168.5.0
net 192.168.4.0
r6:
inter e1/0
ip add 192.168.3.2 255.255.255.0
no sh
inter e1/1
ip add 192.168.4.1 255.255.255.0
no sh
inter loop 1
ip add 172.16.1.1 255.255.255.0
router bgp 2
nei 192.168.3.1 remote-as 2
nei 192.168.4.2 remote-as 2
r6在用route-map改变metric前的bgp路径,可以清楚的看到前往192.168.1.0和192.168.6.0的metric值为0,而且下一跳都为
192.168.2.1
R6(config-if)#do show ip bgp
BGP table version is 14, local router ID is 192.168.1.1
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete
Network Next Hop Metric LocPrf Weight Path
*> 172.16.1.0/24 0.0.0.0 0 32768 i
* i192.168.1.0 192.168.5.2 0 100 0 1 i
*>i 192.168.2.1 0 100 0 1 i
*>i192.168.2.0 192.168.3.1 0 100 0 i
*> 192.168.3.0 0.0.0.0 0 32768 i
* i 192.168.3.1 0 100 0 i
*> 192.168.4.0 0.0.0.0 0 32768 i
* i 192.168.4.2 0 100 0 i
*>i192.168.5.0 192.168.4.2 0 100 0 i
* i192.168.6.0 192.168.5.2 0 100 0 1 i
*>i 192.168.2.1 0 100 0 1 i
用route-map改变metric后,可以看到ibgp邻居192.168.3.1通告的前往192.168.1.0和192.168.6.0的metric分别变为100和200,而
ibgp邻居192.168.4.2通告的前往192.168.1.0和192.168.6.0的metric分别为200和100.med值越小越好这导致r6前往192.168.1.0通过
r2,而前往192.168.6.0通r3.
R6#show ip bgp
BGP table version is 18, local router ID is 192.168.1.1
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete
Network Next Hop Metric LocPrf Weight Path
*> 172.16.1.0/24 0.0.0.0 0 32768 i
* i192.168.1.0 192.168.5.2 200 100 0 1 i
*>i 192.168.2.1 100 100 0 1 i
*>i192.168.2.0 192.168.3.1 0 100 0 i
*> 192.168.3.0 0.0.0.0 0 32768 i
* i 192.168.3.1 0 100 0 i
*> 192.168.4.0 0.0.0.0 0 32768 i
* i 192.168.4.2 0 100 0 i
*>i192.168.5.0 192.168.4.2 0 100 0 i
*>i192.168.6.0 192.168.5.2 100 100 0 1 i
* i 192.168.2.1 200 100 0 1 i
r1上前往172.16.1.0是经过r2的
R1#show ip bgp
BGP table version is 9, local router ID is 192.168.6.2
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete
Network Next Hop Metric LocPrf Weight Path
* i172.16.1.0/24 192.168.5.1 0 100 0 2 i
*>i 192.168.2.2 0 100 0 2 i
* i192.168.1.0 192.168.1.2 0 100 0 i
*> 0.0.0.0 0 32768 i
*>i192.168.2.0 192.168.1.2 0 100 0 i
* i192.168.3.0 192.168.5.1 0 100 0 2 i
*>i 192.168.2.2 0 100 0 2 i
*>i192.168.4.0 192.168.2.2 0 100 0 2 i
* i 192.168.5.1 0 100 0 2 i
*>i192.168.5.0 192.168.6.1 0 100 0 i
* i192.168.6.0 192.168.6.1 0 100 0 i
*> 0.0.0.0 0 32768 i
本地优先级值越大越好,修改优先级后通过r3前往172.16.1.0的本地优先级为200比r2通告的优先级高,所以通过r3前往172.16.1.0
R1#show ip bgp
BGP table version is 10, local router ID is 192.168.6.2
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete
Network Next Hop Metric LocPrf Weight Path
*>i172.16.1.0/24 192.168.5.1 0 200 0 2 i
* i 192.168.2.2 0 100 0 2 i
* i192.168.1.0 192.168.1.2 0 100 0 i
*> 0.0.0.0 0 32768 i
*>i192.168.2.0 192.168.1.2 0 100 0 i
* i192.168.3.0 192.168.5.1 0 100 0 2 i
*>i 192.168.2.2 0 100 0 2 i
*>i192.168.4.0 192.168.2.2 0 100 0 2 i
* i 192.168.5.1 0 100 0 2 i
*>i192.168.5.0 192.168.6.1 0 100 0 i
* i192.168.6.0 192.168.6.1 0 100 0 i
*> 0.0.0.0 0 32768 i