R2(config)#int lo0 R2(config-if)#ip add 2.2.2.2 255.255.255.0 R2(config-if)#ip ospf 1 area 0 R2(config-if)#int e0/0 R2(config-if)#ip add 192.168.12.2 255.255.255.0 R2(config-if)#no sh R2(config-if)#int e0/1 R2(config-if)#ip add 192.168.23.2 255.255.255.0 R2(config-if)#ip ospf 1 area 0 R2(config-if)#no sh R2(config)#router rip R2(config-router)#ver 2 R2(config-router)#no au R2(config-router)#net 192.168.12.0
1 2 3 4 5 6 7
R3(config)#int lo0 R3(config-if)#ip add 3.3.3.3 255.255.255.0 R3(config-if)#ip ospf 1 area 0 R3(config-if)#int e0/0 R3(config-if)#ip add 192.168.23.3 255.255.255.0 R3(config-if)#no sh R3(config-if)#ip ospf 1 area 0
在R2上检查路由条目,确保OSPF和RIP的路由都被学到
1 2 3 4 5 6 7 8 9 10 11 12 13 14
R2#show ip route 1.0.0.0/24 is subnetted, 1 subnets R 1.1.1.0 [120/1] via 192.168.12.1, 00:00:03, Ethernet0/0 2.0.0.0/8 is variably subnetted, 2 subnets, 2 masks C 2.2.2.0/24 is directly connected, Loopback0 L 2.2.2.2/32 is directly connected, Loopback0 3.0.0.0/32 is subnetted, 1 subnets O 3.3.3.3 [110/11] via 192.168.23.3, 00:00:24, Ethernet0/1 192.168.12.0/24 is variably subnetted, 2 subnets, 2 masks C 192.168.12.0/24 is directly connected, Ethernet0/0 L 192.168.12.2/32 is directly connected, Ethernet0/0 192.168.23.0/24 is variably subnetted, 2 subnets, 2 masks C 192.168.23.0/24 is directly connected, Ethernet0/1 L 192.168.23.2/32 is directly connected, Ethernet0/1
R1#sh ip route rip 2.0.0.0/24 is subnetted, 1 subnets R 2.2.2.0 [120/5] via 192.168.12.2, 00:00:26, Ethernet0/0 3.0.0.0/32 is subnetted, 1 subnets R 3.3.3.3 [120/5] via 192.168.12.2, 00:00:26, Ethernet0/0 R 192.168.23.0/24 [120/5] via 192.168.12.2, 00:00:26, Ethernet0/0
R3#sh ip route 1.0.0.0/24 is subnetted, 1 subnets O E2 1.1.1.0 [110/20] via 192.168.23.2, 00:00:04, Ethernet0/0 2.0.0.0/32 is subnetted, 1 subnets O 2.2.2.2 [110/11] via 192.168.23.2, 00:06:23, Ethernet0/0 3.0.0.0/8 is variably subnetted, 2 subnets, 2 masks C 3.3.3.0/24 is directly connected, Loopback0 L 3.3.3.3/32 is directly connected, Loopback0 O E2 192.168.12.0/24 [110/20] via 192.168.23.2, 00:00:04, Ethernet0/0 192.168.23.0/24 is variably subnetted, 2 subnets, 2 masks C 192.168.23.0/24 is directly connected, Ethernet0/0 L 192.168.23.3/32 is directly connected, Ethernet0/0
R1#ping 3.3.3.3 Type escape sequence to abort. Sending 5, 100-byte ICMP Echos to 3.3.3.3, timeout is 2 seconds: !!!!! Success rate is 100 percent (5/5), round-trip min/avg/max = 1/1/1 ms
R2(config)#router rip R2(config-router)#red ospf 1 me 1 R2(config-router)#router ospf 1 R2(config-router)#red rip sub ==================================== R3(config)#router rip R3(config-router)#red ospf 1 me 1 R3(config-router)#router ospf 1 R3(config-router)#red rip su
我们去R2和R3的路由表里面观察5.5.5.0/24这条路由
1 2 3 4 5 6 7
R2#sh ip route 5.0.0.0/24 is subnetted, 1 subnets R 5.5.5.0 [120/2] via 192.168.24.4, 00:00:19, Ethernet0/1 ======================================================== R3#sh ip route 5.0.0.0/24 is subnetted, 1 subnets O E2 5.5.5.0 [110/20] via 192.168.13.1, 00:01:28, Ethernet0/0
R3上的5.5.5.0/24并没有选择最佳路线,而是选择从R1绕一圈。
再观察R4的路由表中5.5.5.0/24
1 2 3 4
R4#sh ip route 5.0.0.0/24 is subnetted, 1 subnets R 5.5.5.0 [120/1] via 192.168.45.5, 00:00:24, Ethernet0/2 [120/1] via 192.168.34.3, 00:00:08, Ethernet0/1
R5#sh ip route 1.0.0.0/24 is subnetted, 1 subnets R 1.1.1.0 [120/1] via 192.168.15.1, 00:00:01, Ethernet0/0 2.0.0.0/24 is subnetted, 1 subnets D 2.2.2.0 [90/409600] via 192.168.25.2, 00:02:09, Ethernet0/1 3.0.0.0/32 is subnetted, 1 subnets O 3.3.3.3 [110/11] via 192.168.35.3, 00:02:24, Ethernet0/2 4.0.0.0/24 is subnetted, 1 subnets i L2 4.4.4.0 [115/20] via 192.168.45.4, 00:01:48, Ethernet0/3 5.0.0.0/8 is variably subnetted, 2 subnets, 2 masks C 5.5.5.0/24 is directly connected, Loopback0 L 5.5.5.5/32 is directly connected, Loopback0 S 10.0.0.0/8 is directly connected, Null0 192.168.15.0/24 is variably subnetted, 2 subnets, 2 masks C 192.168.15.0/24 is directly connected, Ethernet0/0 L 192.168.15.5/32 is directly connected, Ethernet0/0 192.168.25.0/24 is variably subnetted, 2 subnets, 2 masks C 192.168.25.0/24 is directly connected, Ethernet0/1 L 192.168.25.5/32 is directly connected, Ethernet0/1 192.168.35.0/24 is variably subnetted, 2 subnets, 2 masks C 192.168.35.0/24 is directly connected, Ethernet0/2 L 192.168.35.5/32 is directly connected, Ethernet0/2 192.168.45.0/24 is variably subnetted, 2 subnets, 2 masks C 192.168.45.0/24 is directly connected, Ethernet0/3 L 192.168.45.5/32 is directly connected, Ethernet0/3
R1#sh ip route rip 2.0.0.0/24 is subnetted, 1 subnets R 2.2.2.0 [120/5] via 192.168.15.5, 00:00:21, Ethernet0/0 3.0.0.0/32 is subnetted, 1 subnets R 3.3.3.3 [120/5] via 192.168.15.5, 00:00:21, Ethernet0/0 4.0.0.0/24 is subnetted, 1 subnets R 4.4.4.0 [120/5] via 192.168.15.5, 00:00:21, Ethernet0/0 5.0.0.0/24 is subnetted, 1 subnets R 5.5.5.0 [120/1] via 192.168.15.5, 00:00:21, Ethernet0/0 R 10.0.0.0/8 [120/5] via 192.168.15.5, 00:00:14, Ethernet0/0 R 192.168.25.0/24 [120/1] via 192.168.15.5, 00:00:21, Ethernet0/0 R 192.168.35.0/24 [120/1] via 192.168.15.5, 00:00:21, Ethernet0/0 R 192.168.45.0/24 [120/1] via 192.168.15.5, 00:00:21, Ethernet0/0
R1#ping 192.168.1.2 Type escape sequence to abort. Sending 5, 100-byte ICMP Echos to 192.168.1.2, timeout is 2 seconds: !!!!! Success rate is 100 percent (5/5), round-trip min/avg/max = 1/1/2 ms
R3#show ip route ospf 1.0.0.0/32 is subnetted, 1 subnets O 1.1.1.1 [110/11] via 192.168.13.1, 00:00:27, Ethernet0/0 2.0.0.0/8 is variably subnetted, 2 subnets, 2 masks O 2.2.2.2/32 [121/21] via 192.168.13.1, 00:00:27, Ethernet0/0 5.0.0.0/24 is subnetted, 1 subnets O E2 5.5.5.0 [121/20] via 192.168.13.1, 00:00:27, Ethernet0/0 O 192.168.12.0/24 [121/20] via 192.168.13.1, 00:00:27, Ethernet0/0
R3#sh ip route 1.0.0.0/32 is subnetted, 1 subnets O 1.1.1.1 [110/11] via 192.168.13.1, 00:00:53, Ethernet0/0 2.0.0.0/8 is variably subnetted, 2 subnets, 2 masks R 2.2.2.0/24 [120/2] via 192.168.34.4, 00:00:02, Ethernet0/1 O 2.2.2.2/32 [121/21] via 192.168.13.1, 00:00:53, Ethernet0/0 3.0.0.0/8 is variably subnetted, 2 subnets, 2 masks C 3.3.3.0/24 is directly connected, Loopback0 L 3.3.3.3/32 is directly connected, Loopback0 4.0.0.0/24 is subnetted, 1 subnets R 4.4.4.0 [120/1] via 192.168.34.4, 00:00:02, Ethernet0/1 5.0.0.0/24 is subnetted, 1 subnets R 5.5.5.0 [120/2] via 192.168.34.4, 00:00:02, Ethernet0/1 R 192.168.12.0/24 [120/2] via 192.168.34.4, 00:00:02, Ethernet0/1 192.168.13.0/24 is variably subnetted, 2 subnets, 2 masks C 192.168.13.0/24 is directly connected, Ethernet0/0 L 192.168.13.3/32 is directly connected, Ethernet0/0 R 192.168.24.0/24 [120/1] via 192.168.34.4, 00:00:02, Ethernet0/1 192.168.34.0/24 is variably subnetted, 2 subnets, 2 masks C 192.168.34.0/24 is directly connected, Ethernet0/1 L 192.168.34.3/32 is directly connected, Ethernet0/1 R 192.168.45.0/24 [120/1] via 192.168.34.4, 00:00:02, Ethernet0/1
route-map <名字> permit/deny <序列号> match <条件1> # 条件1和条件2都要匹配,and match <条件2> set <动作1> set <动作2> route-map <同一个名字> permit/deny <序列号> match <条件1> <条件2> # 条件1或者条件2有一个匹配就可以,or set <动作1> set <动作2>
特点
使用match命令匹配特定的分组或路由,set修改该分组或路由的相关属性
route-map中的每个序列号是匹配过程中的顺序
route-map默认位permit,默认序列号为10,不会自动递增,建议以十递增
末尾隐含deny any
逐条匹配,一旦命中,就执行动作,不再向下匹配,所以建议将条件严格的放在上面
次优路径和路由倒灌可以用route-map来解决
先还原实验环境,也就是删除上面改管理距离的命令
1 2 3 4 5
R2 route-map r-o permit 10 set tag 666 router ospf 1 redistribute rip subnets route-map r-o
在R1和R3上可以看到这个路由标记
1 2 3 4 5 6 7 8 9
R1#sh ip route 192.168.24.0 Routing entry for 192.168.24.0/24 Known via "ospf 1", distance 110, metric 20 Tag 666, type extern 2, forward metric 10 Last update from 192.168.12.2 on Ethernet0/0, 00:01:33 ago Routing Descriptor Blocks: * 192.168.12.2, from 2.2.2.2, 00:01:33 ago, via Ethernet0/0 Route metric is 20, traffic share count is 1 Route tag 666
在路由条目进入R3 ospf的时候,我们加上过滤
1 2 3 4 5
route-map ospf deny 10 match tag 666 route-map ospf permit 20 router ospf 1 distribute-list route-map ospf in
实验
自己做
分发列表(Distribute-list)
主要用来在进出路由表的时候过滤路由条目
在使用的时候需要明确是in还是out方向
对于距离矢量路由协议
上面红色箭头标注的地方都是可以用分发列表进行过滤
对于链路状态路由协议
由于链路状态路由协议不是传递的路由表,所以只能在上图中红色箭头处使用
在RIP下的使用
1 2 3 4
R2(config)#access-list 1 deny 172.16.1.0 R2(config)#access-list 1 permit any R2(config)#router rip R2(config-router)#distribute-list 1 in
查看R2和R3的路由表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
R2#sh ip route rip 1.0.0.0/24 is subnetted, 1 subnets R 1.1.1.0 [120/1] via 192.168.12.1, 00:00:02, Ethernet0/0 3.0.0.0/24 is subnetted, 1 subnets R 3.3.3.0 [120/1] via 192.168.23.3, 00:00:02, Ethernet0/1 172.16.0.0/24 is subnetted, 1 subnets R 172.16.2.0 [120/1] via 192.168.12.1, 00:00:02, Ethernet0/0 R3#sh ip route rip 1.0.0.0/24 is subnetted, 1 subnets R 1.1.1.0 [120/2] via 192.168.23.2, 00:00:02, Ethernet0/0 2.0.0.0/24 is subnetted, 1 subnets R 2.2.2.0 [120/1] via 192.168.23.2, 00:00:02, Ethernet0/0 172.16.0.0/24 is subnetted, 1 subnets R 172.16.2.0 [120/2] via 192.168.23.2, 00:00:02, Ethernet0/0 R 192.168.12.0/24 [120/1] via 192.168.23.2, 00:00:02, Ethernet0/0
在距离矢量路由协议中,使用in方向,会影响路由器自身和下游所有路由器
使用out方向,只会影响下游路由器
在OSPF下使用
1 2 3 4
R2(config)#access-list 1 deny 172.16.1.1 R2(config)#access-list 1 permit any R2(config)#router ospf 1 R2(config-router)#distribute-list 1 in
查看R2和R3的路由表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
R2#sh ip route ospf 1.0.0.0/32 is subnetted, 1 subnets O 1.1.1.1 [110/11] via 192.168.12.1, 00:00:22, Ethernet0/0 3.0.0.0/32 is subnetted, 1 subnets O 3.3.3.3 [110/11] via 192.168.23.3, 00:00:22, Ethernet0/1 172.16.0.0/32 is subnetted, 1 subnets O 172.16.2.1 [110/11] via 192.168.12.1, 00:00:22, Ethernet0/0 R3#sh ip route ospf 1.0.0.0/32 is subnetted, 1 subnets O 1.1.1.1 [110/21] via 192.168.23.2, 00:00:02, Ethernet0/0 2.0.0.0/32 is subnetted, 1 subnets O 2.2.2.2 [110/11] via 192.168.23.2, 00:00:02, Ethernet0/0 172.16.0.0/32 is subnetted, 2 subnets O 172.16.1.1 [110/21] via 192.168.23.2, 00:00:02, Ethernet0/0 O 172.16.2.1 [110/21] via 192.168.23.2, 00:00:02, Ethernet0/0 O 192.168.12.0/24 [110/20] via 192.168.23.2, 00:00:02, Ethernet0/0
ip prefix-list test permit 192.168.8.0/22 ge 24 le 24
示例
匹配某条特定路由: 192.168.1.0/24
ip prefix-list test permit 192.168.1.0/24
匹配默认路由
ip prefix-list test permit 0.0.0.0/0
匹配所有主机路由
ip prefix-list test permit 0.0.0.0/0 ge 32
匹配所有路由
ip prefix-list test permit 0.0.0.0/0 le 32
偏移列表(Offset-list)
主要用在距离矢量路由协议上,用来在出入路由表的时候增加度量值
1 2 3
R1(config)#access-list 1 permit 1.1.1.0 R1(config)#router rip R1(config-router)#offset-list 1 out 9
在R2上查看路由表
1 2 3
R2#sh ip route rip 1.0.0.0/24 is subnetted, 1 subnets R 1.1.1.0 [120/10] via 192.168.12.1, 00:00:01, Ethernet0/0
策略路由(Policy-based routing)
传统的路由在负载均衡的时候,会按照traffic share count的比例来转发数据
策略路由会根据流量的不同属性,去决定路径
R4作为学校的出口,需要对学校内部的网络流量做一个资源分配
前提条件
1 2 3 4 5
R4#sh ip route 1.0.0.0/32 is subnetted, 1 subnets O 1.1.1.1 [110/21] via 192.168.34.3, 00:03:25, Ethernet0/1 [110/21] via 192.168.24.2, 00:03:35, Ethernet0/0 # 去往目的地必须要有两条路才能分配
1 2 3 4 5 6 7 8 9
R4(config)#access-list 1 permit 192.168.56.0 0.0.0.255 R4(config)#access-list 2 permit 192.168.57.0 0.0.0.255 R4(config)#route-map pbr permit R4(config-route-map)#match ip add 1 R4(config-route-map)#set ip next-hop 192.168.24.2 192.168.34.3 R4(config-route-map)#route-map pbr per 20 R4(config-route-map)#set ip next-hop 192.168.34.3 R4(config)#int e0/2 R4(config-if)#ip policy route-map pbr
测试是否生效
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
R6#traceroute 1.1.1.1 Type escape sequence to abort. Tracing the route to 1.1.1.1 VRF info: (vrf in name/id, vrf out name/id) 1 192.168.56.5 0 msec 0 msec 0 msec 2 192.168.45.4 0 msec 0 msec 0 msec 3 192.168.24.2 1 msec 1 msec 1 msec 4 192.168.12.1 0 msec * 2 msec R7#traceroute 1.1.1.1 Type escape sequence to abort. Tracing the route to 1.1.1.1 VRF info: (vrf in name/id, vrf out name/id) 1 192.168.57.5 1 msec 0 msec 1 msec 2 192.168.45.4 0 msec 1 msec 0 msec 3 192.168.34.3 1 msec 1 msec 1 msec 4 192.168.13.1 1 msec * 1 msec
下面关闭电信,查看流量的走向
1 2 3 4 5 6 7 8 9 10
R4(config)#int e0/0 R4(config-if)#sh R6#traceroute 1.1.1.1 Type escape sequence to abort. Tracing the route to 1.1.1.1 VRF info: (vrf in name/id, vrf out name/id) 1 192.168.56.5 0 msec 0 msec 0 msec 2 192.168.45.4 1 msec 0 msec 0 msec 3 192.168.34.3 1 msec 0 msec 0 msec 4 192.168.13.1 1 msec * 1 msec
网关也可以让管理流量优先有电信
1 2 3
R4(config)#route-map pbr permit 30 R4(config-route-map)#set ip next-hop 192.168.24.2 192.168.34.3 R4(config)#ip local policy route-map pbr
测试
1 2 3 4 5 6
R4#traceroute 1.1.1.1 Type escape sequence to abort. Tracing the route to 1.1.1.1 VRF info: (vrf in name/id, vrf out name/id) 1 192.168.24.2 0 msec 1 msec 0 msec 2 192.168.12.1 0 msec * 0 msec