不同網段之間可以通過路由器進行通信,但若是中間有多個路由器,在兩三台的情況下,我們可以通過配置靜態路由來實現數據轉發。
實驗拓撲
使用兩台路由器直連,每台下面再連接一台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
的實驗。