# 查看路由表
show ip route
# 添加路由: ip route <目标网络> <子网掩码> <下一跳>
ip route 192.168.0.0 255.255.255.0 172.16.1.2
# 删除路由
no ip route 192.168.0.0 255.255.255.0 172.16.1.2
bash
Router0>en
Router0#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area
* - candidate default, U - per-user static route, o - ODR
P - periodic downloaded static route
Gateway of last resort is not set
172.16.0.0/24 is subnetted, 1 subnets
C 172.16.0.0 is directly connected, Serial3/0
C 192.168.0.0/24 is directly connected, FastEthernet0/0
Router0#
Router0#config t
Enter configuration commands, one per line. End with CNTL/Z.
Router0(config)#ip route 172.16.1.0 255.255.255.0 172.16.0.2
Router0(config)#ip route 172.16.2.0 255.255.255.0 172.16.0.2
Router0(config)#ip route 192.168.1.0 255.255.255.0 172.16.0.2
Router0(config)#
Router0#
%SYS-5-CONFIG_I: Configured from console by console
Router0#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area
* - candidate default, U - per-user static route, o - ODR
P - periodic downloaded static route
Gateway of last resort is not set
172.16.0.0/24 is subnetted, 3 subnets
C 172.16.0.0 is directly connected, Serial3/0
S 172.16.1.0 [1/0] via 172.16.0.2
S 172.16.2.0 [1/0] via 172.16.0.2
C 192.168.0.0/24 is directly connected, FastEthernet0/0
S 192.168.1.0/24 [1/0] via 172.16.0.2
Router0#
bash

路由器1:配置静态路由
复制代码
Router1>en
Router1#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area
* - candidate default, U - per-user static route, o - ODR
P - periodic downloaded static route
Gateway of last resort is not set
172.16.0.0/24 is subnetted, 2 subnets
C 172.16.0.0 is directly connected, Serial2/0
C 172.16.1.0 is directly connected, Serial3/0
Router1#
Router1#config t
Enter configuration commands, one per line. End with CNTL/Z.
Router1(config)#ip route 192.168.0.0 255.255.255.0 172.16.0.1
Router1(config)#ip route 172.16.2.0 255.255.255.0 172.16.1.2
Router1(config)#ip route 192.168.1.0 255.255.255.0 172.16.1.2
Router1(config)#
Router1(config)#
Router1#
%SYS-5-CONFIG_I: Configured from console by console
Router1#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area
* - candidate default, U - per-user static route, o - ODR
P - periodic downloaded static route
Gateway of last resort is not set
172.16.0.0/24 is subnetted, 3 subnets
C 172.16.0.0 is directly connected, Serial2/0
C 172.16.1.0 is directly connected, Serial3/0
S 172.16.2.0 [1/0] via 172.16.1.2
S 192.168.0.0/24 [1/0] via 172.16.0.1
S 192.168.1.0/24 [1/0] via 172.16.1.2
Router1#
bash

路由器2:配置静态路由
复制代码
Router2>en
Router2#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area
* - candidate default, U - per-user static route, o - ODR
P - periodic downloaded static route
Gateway of last resort is not set
172.16.0.0/24 is subnetted, 2 subnets
C 172.16.1.0 is directly connected, Serial2/0
C 172.16.2.0 is directly connected, Serial3/0
Router2#
Router2#config t
Enter configuration commands, one per line. End with CNTL/Z.
Router2(config)#ip route 192.168.0.0 255.255.255.0 172.16.1.1
Router2(config)#ip route 172.16.0.0 255.255.255.0 172.16.1.1
Router2(config)#ip route 192.168.1.0 255.255.255.0 172.16.2.2
Router2(config)#
Router2#
%SYS-5-CONFIG_I: Configured from console by console
Router2#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area
* - candidate default, U - per-user static route, o - ODR
P - periodic downloaded static route
Gateway of last resort is not set
172.16.0.0/24 is subnetted, 3 subnets
S 172.16.0.0 [1/0] via 172.16.1.1
C 172.16.1.0 is directly connected, Serial2/0
C 172.16.2.0 is directly connected, Serial3/0
S 192.168.0.0/24 [1/0] via 172.16.1.1
S 192.168.1.0/24 [1/0] via 172.16.2.2
Router2#
bash

路由器3:配置静态路由
复制代码
Router3>en
Router3#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area
* - candidate default, U - per-user static route, o - ODR
P - periodic downloaded static route
Gateway of last resort is not set
172.16.0.0/24 is subnetted, 1 subnets
C 172.16.2.0 is directly connected, Serial2/0
C 192.168.1.0/24 is directly connected, FastEthernet0/0
Router3#
Router3#config t
Enter configuration commands, one per line. End with CNTL/Z.
Router3(config)#ip route 192.168.0.0 255.255.255.0 172.16.2.1
Router3(config)#ip route 172.16.0.0 255.255.255.0 172.16.2.1
Router3(config)#ip route 172.16.1.0 255.255.255.0 172.16.2.1
Router3(config)#
Router3#
%SYS-5-CONFIG_I: Configured from console by console
Router#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area
* - candidate default, U - per-user static route, o - ODR
P - periodic downloaded static route
Gateway of last resort is not set
172.16.0.0/24 is subnetted, 3 subnets
S 172.16.0.0 [1/0] via 172.16.2.1
S 172.16.1.0 [1/0] via 172.16.2.1
C 172.16.2.0 is directly connected, Serial2/0
S 192.168.0.0/24 [1/0] via 172.16.2.1
C 192.168.1.0/24 is directly connected, FastEthernet0/0
Router3#
bash

# 验证命令
# 查看路由表中的RIP路由
show ip route rip
# 查看RIP邻居信息
show ip protocols
# 开启实时监控路由更新
# 显示发送和接收到的RIP路由更新信息,显示路由器使用了RIP的V1版还是V2版本
debug ip rip
# 关闭实时监控路由更新
no debug ip rip
bash

3.7.4.2.10 RIP配置案例
1. 搭建实验环境
2. 路由器配置RIP
以路由器2为例,路由器0~4均按照如下命令配置RIP。
复制代码
Router2>
Router2>en
Router2#config t
Enter configuration commands, one per line. End with CNTL/Z.
Router2(config)#router rip
Router2(config-router)#network 172.16.0.0
Router2(config-router)#
Router2(config-router)#
bash
3. 查看路由表
以路由器2为例,路由器2动态学到了4条路由(2条直连路由除外)。
复制代码
Router2#
Router2#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area
* - candidate default, U - per-user static route, o - ODR
P - periodic downloaded static route
Gateway of last resort is not set
172.16.0.0/24 is subnetted, 6 subnets
C 172.16.0.0 is directly connected, FastEthernet0/0
R 172.16.1.0 [120/2] via 172.16.2.2, 00:00:25, Serial2/0
C 172.16.2.0 is directly connected, Serial2/0
R 172.16.3.0 [120/1] via 172.16.2.2, 00:00:25, Serial2/0
R 172.16.4.0 [120/1] via 172.16.2.2, 00:00:25, Serial2/0
R 172.16.5.0 [120/2] via 172.16.2.2, 00:00:23, Serial2/0
Router2#
bash

# 查看 OSPF 邻居
show ip ospf neighbor
# 查看 OSPF 路由表
show ip route ospf
# 查看 OSPF 协议状态
show ip ospf
# 查看 OSPF 链路状态数据库(LSDB)
show ip ospf database
bash

3.7.4.3.5 OSPF协议配置案例
1. 搭建实验环境
2. 路由器配置OSPF
路由器2配置OSPF
复制代码
Router2>en
Router2#config t
Enter configuration commands, one per line. End with CNTL/Z.
Router2(config)#router ospf ?
<1-65535> Process ID
Router2(config)#router ospf 1
Router2(config-router)#network 172.16.0.0 0.0.255.255 area 0
Router2(config-router)#network 192.168.0.0 0.0.0.3 area 0
Router2(config-router)#
bash
路由器0配置OSPF
方式一:三个直连网络合并成一个C类网络(推荐)
复制代码
Router0>
Router0>en
Router0#config t
Enter configuration commands, one per line. End with CNTL/Z.
Router0(config)#router ospf ?
<1-65535> Process ID
Router0(config)#router ospf 1
Router0(config-router)#network 192.168.0.0 0.0.0.255 area 0
Router0(config-router)#
bash
方式二:逐个添加三个直连网络
复制代码
Router0>en
Router0#config ?
terminal Configure from the terminal
<cr>
Router0#config t
Enter configuration commands, one per line. End with CNTL/Z.
Router0(config)#router ospf ?
<1-65535> Process ID
Router0(config)#router ospf 1
Router0(config-router)#network 192.168.0.0 0.0.0.3 area 0
Router0(config-router)#network 192.168.0.4 0.0.0.3 area 0
Router0(config-router)#
00:30:56: %OSPF-5-ADJCHG: Process 1, Nbr 192.168.0.1 on Serial3/0 from LOADING to FULL, Loading Done
Router0(config-router)#network 192.168.0.12 0.0.0.3 area 0
Router0(config-router)#
bash

路由器1配置OSPF
复制代码
Router1>
Router1>en
Router1#config t
Enter configuration commands, one per line. End with CNTL/Z.
Router1(config)#router ospf ?
<1-65535> Process ID
Router1(config)#router ospf 1
Router1(config-router)#network 192.168.0.0 0.0.0.255 area 0
Router1(config-router)#
bash
路由器3配置OSPF
复制代码
Router3>en
Router3#config t
Enter configuration commands, one per line. End with CNTL/Z.
Router3(config)#router ospf ?
<1-65535> Process ID
Router3(config)#router ospf 1
Router3(config-router)#network 192.168.0.0 0.0.0.255 area 0
Router3(config-router)#network 172.17.0.0 0.0.255.255 area 0
Router3(config-router)#
bash
路由器4配置OSPF
复制代码
Router4>en
Router4#config t
Enter configuration commands, one per line. End with CNTL/Z.
Router4(config)#router ospf ?
<1-65535> Process ID
Router4(config)#router ospf 1
Router4(config-router)#network 192.168.0.0 0.0.0.255 area 0
Router4(config-router)#network 172.18.0.0 0.0.255.255 area 0
Router4(config-router)#
bash
3. 查看路由表
以路由器2为例,路由器2动态学到了6条路由(2条直连路由除外)。
复制代码
Router2>en
Router2#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area
* - candidate default, U - per-user static route, o - ODR
P - periodic downloaded static route
Gateway of last resort is not set
C 172.16.0.0/16 is directly connected, FastEthernet0/0
O 172.17.0.0/16 [110/129] via 192.168.0.2, 00:30:35, Serial2/0
O 172.18.0.0/16 [110/193] via 192.168.0.2, 00:43:14, Serial2/0
192.168.0.0/30 is subnetted, 5 subnets
C 192.168.0.0 is directly connected, Serial2/0
O 192.168.0.4 [110/128] via 192.168.0.2, 00:43:24, Serial2/0
O 192.168.0.8 [110/192] via 192.168.0.2, 00:43:14, Serial2/0
O 192.168.0.12 [110/128] via 192.168.0.2, 00:30:45, Serial2/0
O 192.168.0.16 [110/192] via 192.168.0.2, 00:03:54, Serial2/0
Router2#
bash

4. 验证OSPF协议健壮性
主机0 tracert 主机1,观察数据包的路径。初始数据包路径如下:
复制代码
PC>
PC>ping 172.17.0.2
Pinging 172.17.0.2 with 32 bytes of data:
Request timed out.
Reply from 172.17.0.2: bytes=32 time=4ms TTL=125
Reply from 172.17.0.2: bytes=32 time=8ms TTL=125
Reply from 172.17.0.2: bytes=32 time=2ms TTL=125
Ping statistics for 172.17.0.2:
Packets: Sent = 4, Received = 3, Lost = 1 (25% loss),
Approximate round trip times in milli-seconds:
Minimum = 2ms, Maximum = 8ms, Average = 4ms
PC>
PC>
PC>tracert 172.17.0.2
Tracing route to 172.17.0.2 over a maximum of 30 hops:
1 1 ms 0 ms 0 ms 172.16.0.1
2 1 ms 8 ms 6 ms 192.168.0.2
3 1 ms 2 ms 1 ms 192.168.0.14
4 2 ms 0 ms 0 ms 172.17.0.2
Trace complete.
PC>
bash

当断开最佳连接,跟踪数据包路径,断开后会立即更换路径。
复制代码
PC>
PC>ping 172.17.0.2
Pinging 172.17.0.2 with 32 bytes of data:
Reply from 172.17.0.2: bytes=32 time=4ms TTL=123
Reply from 172.17.0.2: bytes=32 time=49ms TTL=123
Reply from 172.17.0.2: bytes=32 time=4ms TTL=123
Reply from 172.17.0.2: bytes=32 time=5ms TTL=123
Ping statistics for 172.17.0.2:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 4ms, Maximum = 49ms, Average = 15ms
PC>tracert 172.17.0.2
Tracing route to 172.17.0.2 over a maximum of 30 hops:
1 0 ms 0 ms 0 ms 172.16.0.1
2 1 ms 0 ms 2 ms 192.168.0.2
3 0 ms 0 ms 1 ms 192.168.0.6
4 11 ms 4 ms 2 ms 192.168.0.10
5 9 ms 2 ms 11 ms 192.168.0.18
6 3 ms 0 ms 2 ms 172.17.0.2
Trace complete.
PC>
bash

当恢复最佳连接,跟踪数据包路径,恢复后需等待一段时间,路径才会变成回最佳路径。
复制代码
PC>
PC>tracert 172.17.0.2
Tracing route to 172.17.0.2 over a maximum of 30 hops:
1 0 ms 0 ms 0 ms 172.16.0.1
2 0 ms 1 ms 1 ms 192.168.0.2
3 2 ms 19 ms 4 ms 192.168.0.6
4 1 ms 1 ms 0 ms 192.168.0.10
5 4 ms 3 ms 4 ms 192.168.0.18
6 5 ms 3 ms 3 ms 172.17.0.2
Trace complete.
PC>tracert 172.17.0.2
Tracing route to 172.17.0.2 over a maximum of 30 hops:
1 0 ms 1 ms 0 ms 172.16.0.1
2 0 ms 0 ms 0 ms 192.168.0.2
3 0 ms 1 ms 1 ms 192.168.0.6
4 8 ms 1 ms 1 ms 192.168.0.10
5 2 ms 1 ms 2 ms 192.168.0.14
6 1 ms 3 ms 2 ms 172.17.0.2
Trace complete.
PC>tracert 172.17.0.2
Tracing route to 172.17.0.2 over a maximum of 30 hops:
1 1 ms 0 ms 0 ms 172.16.0.1
2 0 ms 1 ms 7 ms 192.168.0.2
3 1 ms 6 ms 5 ms 192.168.0.6
4 9 ms 0 ms 1 ms 172.17.0.2
Trace complete.
PC>
bash
