不同网段之间可以通过路由器进行通信,但若是中间有多个路由器,在两三台的情况下,我们可以通过配置静态路由来实现数据转发。
实验拓扑
使用两台路由器直连,每台下面再连接一台VPC
。地址规划如下:
名称 | 接口 | IP地址 |
---|---|---|
R1 | f0/0 | 192.168.30.1/24 |
R1 | f0/1 | 192.168.10.1/24 |
R2 | f0/0 | 192.168.30.2/24 |
R2 | f0/1 | 192.168.20.1/24 |
PC1 | e0 | 192.168.10.10/24 |
PC2 | e0 | 192.168.20.10/24 |
静态路由
配置方法
全局模式下添加一条静态路由。
R1#configure terminal
R1(config)#ip route 192.168.20.0 255.255.255.0 192.168.30.2
- 192.168.20.0:目的网段
- 255.255.255.0:子网掩码
- 192.168.30.2:下一跳
配置过程
R1
- 配置
f0/1
接口ip
地址
R1#configure terminal
R1(config)#interface fastEthernet 0/1
R1(config-if)#ip address 192.168.10.1 255.255.255.0
R1(config-if)#no shutdown
R1(config-if)#exit
- 配置
f0/0
接口ip
地址
R1(config)#interface fastEthernet 0/0
R1(config-if)#ip address 192.168.30.1 255.255.255.0
R1(config-if)#no shutdown
R1(config-if)#end
- 查看接口地址及状态
R1#show ip interface brief
Interface IP-Address OK? Method Status Protocol
FastEthernet0/0 192.168.30.1 YES manual up up
FastEthernet0/1 192.168.10.1 YES manual up up
- 查看路由表
R1#show ip route
Codes: C - connected, S - static, 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
i - IS-IS, su - IS-IS summary, 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 192.168.30.0/24 is directly connected, FastEthernet0/0
C 192.168.10.0/24 is directly connected, FastEthernet0/1
- 添加到达
192.168.20.0
网段的静态路由
R1#configure terminal
R1(config)#ip route 192.168.20.0 255.255.255.0 192.168.30.2
R1(config)#end
- 再次查看路由表
R1#show ip route
Codes: C - connected, S - static, 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
i - IS-IS, su - IS-IS summary, 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 192.168.30.0/24 is directly connected, FastEthernet0/0
C 192.168.10.0/24 is directly connected, FastEthernet0/1
S 192.168.20.0/24 [1/0] via 192.168.30.2
R2
R2
与R1
的配置方法相同,以下为简写配置过程。
R2#conf t
R2(config)#int f0/1
R2(config-if)#ip add 192.168.20.1 255.255.255.0
R2(config-if)#no sh
R2(config-if)#int f0/0
R2(config-if)#ip add 192.168.30.2 255.255.255.0
R2(config-if)#no sh
R2(config-if)#ex
R2(config)#ip route 192.168.10.0 255.255.255.0 192.168.30.1
R2(config)#do show ip int b
Interface IP-Address OK? Method Status Protocol
FastEthernet0/0 192.168.30.2 YES manual up up
FastEthernet0/1 192.168.20.1 YES manual up up
R2(config)#do show ip route
Codes: C - connected, S - static, 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
i - IS-IS, su - IS-IS summary, 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 192.168.30.0/24 is directly connected, FastEthernet0/0
S 192.168.10.0/24 [1/0] via 192.168.30.1
C 192.168.20.0/24 is directly connected, FastEthernet0/1
PING 测试
配置 IP 地址
PC1> ip 192.168.10.10 255.255.255.0 192.168.10.1
Checking for duplicate address...
PC1 : 192.168.10.10 255.255.255.0 gateway 192.168.10.1
PC2> ip 192.168.20.10 192.168.20.1
Checking for duplicate address...
PC1 : 192.168.20.10 255.255.255.0 gateway 192.168.20.1
PING
PC1> ping 192.168.20.10
192.168.20.10 icmp_seq=1 timeout
192.168.20.10 icmp_seq=2 timeout
84 bytes from 192.168.20.10 icmp_seq=3 ttl=62 time=41.947 ms
84 bytes from 192.168.20.10 icmp_seq=4 ttl=62 time=39.882 ms
84 bytes from 192.168.20.10 icmp_seq=5 ttl=62 time=42.885 ms
PC2> ping 192.168.10.10
84 bytes from 192.168.10.10 icmp_seq=1 ttl=62 time=38.025 ms
84 bytes from 192.168.10.10 icmp_seq=2 ttl=62 time=39.887 ms
84 bytes from 192.168.10.10 icmp_seq=3 ttl=62 time=43.782 ms
84 bytes from 192.168.10.10 icmp_seq=4 ttl=62 time=32.903 ms
84 bytes from 192.168.10.10 icmp_seq=5 ttl=62 time=38.942 ms
通过配置静态路由成功使不同网段之间互通。
默认路由
默认路由是一种特殊的静态路由,当配置了默认路由,转发数据包中目的地址在路由表中没有相匹配的表项时,数据包将按默认路由转发,否则数据包会被丢弃。
设想一:在
R2
上只配一条默认路由,是否可行?
设想二:R1
和R2
都只配一条默认路由,是否可行?
配置方法
全局模式下添加一条默认路由。
R2(config)#ip route 0.0.0.0 0.0.0.0 192.168.30.1
- 0.0.0.0 0.0.0.0:代表任何网络
- 192.168.30.1:下一跳
设想一
配置过程
R2
删除添加的静态路由,添加默认路由。
R2(config)#no ip route 192.168.10.0 255.255.255.0
R2(config)#ip route 0.0.0.0 0.0.0.0 192.168.30.1
R2(config)#do show ip route
Codes: C - connected, S - static, 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
i - IS-IS, su - IS-IS summary, 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 192.168.30.1 to network 0.0.0.0
C 192.168.30.0/24 is directly connected, FastEthernet0/0
C 192.168.20.0/24 is directly connected, FastEthernet0/1
S* 0.0.0.0/0 [1/0] via 192.168.30.1
PING 测试
PC1> ping 192.168.20.10
192.168.20.10 icmp_seq=1 timeout
192.168.20.10 icmp_seq=2 timeout
84 bytes from 192.168.20.10 icmp_seq=3 ttl=62 time=37.905 ms
84 bytes from 192.168.20.10 icmp_seq=4 ttl=62 time=34.870 ms
84 bytes from 192.168.20.10 icmp_seq=5 ttl=62 time=42.728 ms
PC2> ping 192.168.10.10
84 bytes from 192.168.10.10 icmp_seq=1 ttl=62 time=38.995 ms
84 bytes from 192.168.10.10 icmp_seq=2 ttl=62 time=35.875 ms
84 bytes from 192.168.10.10 icmp_seq=3 ttl=62 time=36.911 ms
84 bytes from 192.168.10.10 icmp_seq=4 ttl=62 time=37.904 ms
84 bytes from 192.168.10.10 icmp_seq=5 ttl=62 time=35.912 ms
通过
PING
测试,发现是可行的。
设想二
配置过程
- 再在
R1
上删除添加的静态路由,添加默认路由。
R1(config)#no ip route 192.168.20.0 255.255.255.0
R1(config)#ip route 0.0.0.0 0.0.0.0 192.168.30.2
R1(config)#do show ip route
Codes: C - connected, S - static, 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
i - IS-IS, su - IS-IS summary, 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 192.168.30.2 to network 0.0.0.0
C 192.168.30.0/24 is directly connected, FastEthernet0/0
C 192.168.10.0/24 is directly connected, FastEthernet0/1
S* 0.0.0.0/0 [1/0] via 192.168.30.2
PING 测试
PC1> ping 192.168.20.10
192.168.20.10 icmp_seq=1 timeout
192.168.20.10 icmp_seq=2 timeout
84 bytes from 192.168.20.10 icmp_seq=3 ttl=62 time=33.073 ms
84 bytes from 192.168.20.10 icmp_seq=4 ttl=62 time=36.042 ms
84 bytes from 192.168.20.10 icmp_seq=5 ttl=62 time=41.983 ms
PC2> ping 192.168.10.10
84 bytes from 192.168.10.10 icmp_seq=1 ttl=62 time=43.736 ms
84 bytes from 192.168.10.10 icmp_seq=2 ttl=62 time=42.030 ms
84 bytes from 192.168.10.10 icmp_seq=3 ttl=62 time=39.927 ms
84 bytes from 192.168.10.10 icmp_seq=4 ttl=62 time=34.038 ms
84 bytes from 192.168.10.10 icmp_seq=5 ttl=62 time=40.854 ms
好像也可以哦,没问题;若是不小心
ping
了一个不存在的地址呢?
PC1> ping 192.168.40.10
*192.168.30.2 icmp_seq=1 ttl=254 time=698.062 ms (ICMP type:11, code:0, TTL expired in transit)
*192.168.30.2 icmp_seq=2 ttl=254 time=705.060 ms (ICMP type:11, code:0, TTL expired in transit)
*192.168.30.2 icmp_seq=3 ttl=254 time=707.124 ms (ICMP type:11, code:0, TTL expired in transit)
*192.168.30.2 icmp_seq=4 ttl=254 time=702.108 ms (ICMP type:11, code:0, TTL expired in transit)
*192.168.30.2 icmp_seq=5 ttl=254 time=694.003 ms (ICMP type:11, code:0, TTL expired in transit)
PC2> ping 192.168.40.10
*192.168.30.1 icmp_seq=1 ttl=254 time=700.994 ms (ICMP type:11, code:0, TTL expired in transit)
*192.168.30.1 icmp_seq=2 ttl=254 time=704.114 ms (ICMP type:11, code:0, TTL expired in transit)
*192.168.30.1 icmp_seq=3 ttl=254 time=698.020 ms (ICMP type:11, code:0, TTL expired in transit)
*192.168.30.1 icmp_seq=4 ttl=254 time=700.092 ms (ICMP type:11, code:0, TTL expired in transit)
*192.168.30.1 icmp_seq=5 ttl=254 time=694.289 ms (ICMP type:11, code:0, TTL expired in transit)
发现个问题,
TTL
传输中过期,说明数据包在两个路由器之间被互相转发,直到数据包TTL
为0
,这样势必会浪费路由器的资源,对正常的运作产生影响,所以这是不可行的。
最后
关于静态路由的相关实验到此结束,下一篇关于VLAN
的实验。