您好,欢迎来到华佗健康网。
搜索
您的当前位置:首页chp3静态路由

chp3静态路由

来源:华佗健康网
第3章 静态路由

转发数据包是路由器的最主要功能。路由器转发数据包时需要查找路由表,管理员可以通过手工的方法在路由器中直接配置路由表,这就是静态路由。虽然静态路由不适合于在大的网络中使用,但是由于静态路由简单、路由器负载小、可控性强等原因,在许多场合中还经常被使用。本章将介绍静态路由的配置,同时为以后配置动态路由奠定基础。

3.1 静态路由与默认路由

3.1.1 静态路由介绍

路由器在转发数据时,要先在路由表(routing table)中查找相应的路由。路由器有这么三种途径建立路由:

(1) 直连网络:路由器自动添加和自己直接连接的网络的路由 (2) 静态路由:管理员手动输入到路由器的路由

(3) 动态路由:由路由协议(routing protocol)动态建立的路由 静态路由的缺点是不能动态反映网络拓扑,当网络拓扑发生变化时,管理员就必须手工改变路由表;然而静态路不会占用路由器太多的CPU和RAM资源,也不占用线路的带宽。如果出于安全的考虑想隐藏网络的某些部分或者管理员想控制数据转发路径,也会使用静态路由。在一个小而简单的网络中,也常使用静态路由,因为配置静态路由会更为简捷。

配置静态路由的命令为“ip route”,命令的格式如下: 接口比ip地址刚好,适合ip地址变化ip route 目的网络 掩码 { 网关地址 | 接口 }

例子:ip route 192.168.1.0 255.255.255.0 s0/0 例子:ip route 192.168.1.0 255.255.255.0 12.12.12.2

在写静态路由时,如果链路是点到点的链路(例如PPP封装的链路),采用网关地址和接口都是可以的;然而如果链路是多路访问的链路(例如以太网),则只能采用网关地址,即不能:ip route 192.168.1.0 255.255.255.0 f0/0 。

【提示】有的IOS版本中,采用ip route 192.168.1.0 255.255.255.0 f0/0时,路由器也是正常工作的,然而这是代理ARP的功劳,建议不要采用该形式。

在路由器上,可以使用“show ip route”命令查看路由表。如下: R1#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

R 172.16.0.0/16 [120/2] via 10.1.0.2, 00:00:21, Serial0/0

[120/2] via 10.3.0.2, 00:00:06, Serial0/1 10.0.0.0/16 is subnetted, 4 subnets

R 10.2.0.0 [120/1] via 10.1.0.2, 00:00:21, Serial0/0 C 10.3.0.0 is directly connected, Serial0/1 C 10.1.0.0 is directly connected, Serial0/0

R 10.4.0.0 [120/1] via 10.3.0.2, 00:00:06, Serial0/1 C 192.168.1.0/24 is directly connected, FastEthernet0/0

在输出中,首先显示路由条目各种类型的的简写,如:“C”为直连网络,“S”为静态路由。以上面带有下划的路由为例,“R”表示这条路由是“RIP”协议学习得到的;“10.2.0.0”是目的网络;“[120/1]”是管理距离(Administrative Distance ,AD)/ 度量值(Metric);“via 10.1.0.2”是指到达目的网络的下一跳路由器的IP地址;“00:00:21”是指路由器最近一次得知路由到现在的时间;“Serials 0/0”是指到达下一跳应从哪个端口出去。

【技术要点】管理距离(AD):用来表示路由的可信度,路由器可能从多种途径获得同一路由,例如:一个路由器要获得“10.2.0.0/24”网络的路由,可以来自RIP,也可以是静态路由。不同途径获得的路由可能采取不同的路径到达目的网络,为了区别它们的可信度,用管理距离加以表示。表3-1是通过各种路由协议获得的路由的默认管理距离。路由表中管理距离值越小说明路由的可靠程度越高,静态路由的管理距离为1,说明手工输入的路由优先级高于其他的路由。

表 3-1 路由协议的默认管理距离

路由协议 直连接口 静态路由 外部BGP 内部EIGRP IGRP OSPF RIP 外部EIGRP 内部BGP

管理距离

0 1 20 90 100 110 120 170 200

【技术要点】度量值(Metric):某一个路由协议判别到目的网络的最佳路径的方法。当一路由器有多条路径到达某一目的网络时,路由协议必须判断其中的哪一条是最佳的并把它放到路由表中,路由协议会给每一条路径计算出一个数,这个数就是度量值,通常这个值是没有单位的。度量值越小,这条路径越佳。然而不同的路由协议定义度量值的方法是不一样的,所以不同的路由协议选择出的最佳距离可能是不一样的。具体请参见路由协议的章节。

3.1.2 默认路由介绍

所谓的默认路由,是指路由器在路由表中如果找不到到达目的网络的具体路由时,最后会采用的路由。默认路由通常会在存根网络(Stub network,即只有一个出口的网络)中使用。如图3-1,图中左边的网络到Internet上只有一个出口,因此可以在R2上配置默认路由。命令为:ip route 0.0.0.0 0.0.0.0 { 网关地址 | 接口 } 例子:ip route 0.0.0.0 0.0.0.0 s0/0

例子:ip route 0.0.0.0 0.0.0.0 12.12.12.2

图3-1 桩网络(Stub network)

3.1.3 ip classless

图3-2 ip classless示例

图3-2中,如果在R1上配置了默认路由:ip route 0.0.0.0 0.0.0.0 s0/0/0,则R1路由器是否会把到达10.2.2.0/24网络的数据从s0/0/0接口发送出去?这取决于是否执行了“ip classless”命令。如果执行了“ip classless”命令(实际上这是默认值),则路由器存在默认路由时,所有在路由表中查不到具体路由的数据包将通过默认路由发送。

如果执行了“no ip classless”命令,当路由器存在一主类网络的某一子网路由时,路由器将认为自己已经知道该主类网络的全部子网的路由,这时即使存在默认路由,到达该主类任一子网的数据包不会通过默认路由发送。图3-2中,执行了“no ip classless”后,由于R1路由器上有10.0.0.0的子网10.1.1.0/24(这是直连路由),因此R1路由器收到到达10.2.2.0/24子网的数据包不会使用默认路由进行发送。然而如果数据包是要到达20.2.2.0/24,默认路由会被采用,因为R1没有任何20.0.0.0子网的路由。

3.2 实验1:静态路由

1. 实验目的

通过本实验,读者可以掌握如下技能: (1) 路由表的概念

(2) ip route命令的使用

(3) 根据需求正确配置静态路由

2. 实验拓扑

图3-3 实验1、实验2拓扑图

3. 实验步骤

我们要使得1.1.1.0/24、2.2.2.0/24、3.3.3.0/24网络之间能够互相通信。 (1) 步骤1:在各路由器上配置IP地址、保证直连链路的连通性 R1(config)#int loopback0

R1(config-if)#ip address 1.1.1.1 255.255.255.0 R1(config)#int s0/0/0

R1(config-if)#ip address 192.168.12.1 255.255.255.0 R1(config-if)#no shutdown

R2(config)#int loopback0

R2(config-if)#ip address 2.2.2.2 255.255.255.0 R2(config)#int s0/0/0

R2(config-if)#clock rate 128000

R2(config-if)#ip address 192.168.12.2 255.255.255.0 R2(config-if)#no shutdown R2(config)#int s0/0/1

R2(config-if)#clock rate 128000

R2(config-if)#ip address 192.168.23.2 255.255.255.0 R2(config-if)#no shutdown

R3(config)#int loopback0

R3(config-if)#ip address 3.3.3.3 255.255.255.0 R3(config)#int s0/0/1

R3(config-if)#ip address 192.168.23.3 255.255.255.0 R3(config-if)#no shutdown

(2) 步骤2:R1上配置静态路由

R1(config)#ip route 2.2.2.0 255.255.255.0 s0/0/0

//下一跳为接口形式,s0/0/0是点对点的链路,注意应该是R1上的s0/0/0接口 R1(config)#ip route 3.3.3.0 255.255.255.0 192.168.12.2 //下一跳为IP地址形式,192.168.12.2是R2上的IP地址 (3) 步骤3:R2上配置静态路由

R2(config)#ip route 1.1.1.0 255.255.255.0 s0/0/0 R2(config)#ip route 3.3.3.0 255.255.255.0 s0/0/1 (4) 步骤4:R3上配置静态路由

R3(config)#ip route 1.1.1.0 255.255.255.0 s0/0/1 R3(config)#ip route 2.2.2.0 255.255.255.0 s0/0/1

4. 实验调试

(1) 在R1、R2、R3上查看路由表 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.12.0/24 is directly connected, Serial0/0/0 1.0.0.0/24 is subnetted, 1 subnets

C 1.1.1.0 is directly connected, Loopback0 2.0.0.0/24 is subnetted, 1 subnets

S 2.2.2.0 is directly connected, Serial0/0/0 3.0.0.0/24 is subnetted, 1 subnets S 3.3.3.0 [1/0] via 192.168.12.2

R2#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.12.0/24 is directly connected, Serial0/0/0 1.0.0.0/24 is subnetted, 1 subnets

S 1.1.1.0 is directly connected, Serial0/0/0 2.0.0.0/24 is subnetted, 1 subnets

C 2.2.2.0 is directly connected, Loopback0 3.0.0.0/24 is subnetted, 1 subnets

S 3.3.3.0 is directly connected, Serial0/0/1 C 192.168.23.0/24 is directly connected, Serial0/0/1

R3#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 1.0.0.0/24 is subnetted, 1 subnets

S 1.1.1.0 is directly connected, Serial0/0/1 2.0.0.0/24 is subnetted, 1 subnets

S 2.2.2.0 is directly connected, Serial0/0/1 3.0.0.0/24 is subnetted, 1 subnets

C 3.3.3.0 is directly connected, Loopback0 C 192.168.23.0/24 is directly connected, Serial0/0/1

(2) 从各路由器的环回口ping 其他路由器的环回口: R1#ping

//不带任何参数的ping命令,允许我们输入更多的参数

Protocol [ip]:

Target IP address: 2.2.2.2 //目标IP地址 Repeat count [5]:

//发送的ping次数 //ping包的大小 //超时时间

//是否进一步扩展命令

//源IP地址

Datagram size [100]: Timeout in seconds [2]: Extended commands [n]: y Type of service [0]:

Set DF bit in IP header? [no]: Validate reply data? [no]: Data pattern [0xABCD]:

Loose, Strict, Record, Timestamp, Verbose[none]: Sweep range of sizes [n]: Type escape sequence to abort.

Sending 5, 100-byte ICMP Echos to 2.2.2.2, timeout is 2 seconds: Packet sent with a source address of 1.1.1.1 !!!!!

Success rate is 100 percent (5/5), round-trip min/avg/max = 12/14/16 ms

Source address or interface: 1.1.1.1

//以上说明从R1的loopback0可以ping通R2上的loopback0。也可以直接使用命令: R1#ping 2.2.2.2 source loopback 0

Type escape sequence to abort.

Sending 5, 100-byte ICMP Echos to 2.2.2.2, timeout is 2 seconds: Packet sent with a source address of 1.1.1.1 !!!!!

Success rate is 100 percent (5/5), round-trip min/avg/max = 12/14/16 ms

R2#ping 1.1.1.1 source loopback 0 R2#ping 3.3.3.3 source loopback 0

//从R2的loopback0应该可以ping通R1和R3的lopback0接口。

R3#ping 1.1.1.1 source loopback 0 R3#ping 2.2.2.2 source loopback 0

//从R3的loopback0也应该可以ping通R1和R2的lopback0接口。

【提示】虽然从R1的loopback0 可以ping通R3的loopback0,数据需要经过192.168.23.0/24网络,但是在R1上我们并没有添加192.168.23.0/24的路由。路由器转发数据包完成是根据路由表的,并且数据是一跳一跳地被转发的,就像接力赛似的。从R1的loopback0口ping R3的loopback0口时,IP数据包的源IP为1.1.1.1,目的IP为3.3.3.3。R1路由器首先查路由表,数据包被发到了R2;R2路由器也查路由表(3.3.3.0/24路由),

数据包被发到了R3;R3知道这是直连路由。R3响应R1的数据包进行类似的过程。 (3) 从R1上ping 2.2.2.2、从R1上ping 3.3.3.3 R1#ping 2.2.2.2

Type escape sequence to abort.

Sending 5, 100-byte ICMP Echos to 2.2.2.2, timeout is 2 seconds: !!!!!

Success rate is 100 percent (5/5), round-trip min/avg/max = 12/14/16 ms

//可以ping通。

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 0 percent (0/5)

//以上无法ping通,原因在于使用ping命令时,如果不指明源接口,则R1路由器使用s0/0/0接口的IP地址(192.168.12.1)作为IP数据包的源IP地址了。R3上响应R1的数据包时,数据包是发向192.168.12.1的,然而由于R3没有192.168.12.0/24的路由,数据包无法发送。即:数据包从R1到了R3后,无法返回R1。

3.3 实验2:默认路由

1. 实验目的

通过本实验,读者可以掌握如下技能: (1) 默认路由的使用场合 (2) 默认路由的配置

2. 实验拓扑

如图3-3。

3. 实验步骤

在实验1的基础上进行实验2。

(1) 步骤1:R1、R3上删除原有静态路由

R1(config)#no ip route 2.2.2.0 255.255.255.0 Serial0/0/0 //要删除路由,在原有命令前面加no即可

R1(config)#no ip route 3.3.3.0 255.255.255.0 192.168.12.2

R3(config)#no ip route 1.1.1.0 255.255.255.0 Serial0/0/1 R3(config)#no ip route 2.2.2.0 255.255.255.0 Serial0/0/1 (2) 步骤2: R1、R3上配置默认路由

R1(config)#ip route 0.0.0.0 0.0.0.0 s0/0/0 R3(config)#ip route 0.0.0.0 0.0.0.0 s0/0/1

4. 实验调试

从各路由器的环回口ping 其他路由器的环回口。请读者比较两个实验ping的结果,

仔细分析原因。

3.4 实验3:ip classless

1. 实验目的

通过本实验,读者可以掌握如下技能: (1) ip classless命令的含义 (2) 配置 ip classless

2. 实验拓扑

图3-4 实验3拓扑图

3. 实验步骤

(1) 步骤1:执行“ip classless” R1(config)#interface Loopback0

R1(config-if)#ip address 10.1.1.1 255.255.255.0 R1(config)#interface Serial0/0/0 R1(config-if)#no shutdown

R1(config-if)#ip address 192.168.12.1 255.255.255.0 R1(config)#ip classless

R1(config)#ip route 0.0.0.0 0.0.0.0 Serial0/0/0 //以上我们配置了默认路由;同时打开“ip classless”,默认就是打开的。

R2(config)#interface Loopback0

R2(config-if)#ip address 10.2.2.2 255.255.255.0 R2(config)#interface Serial0/0/0 R1(config-if)#no shutdown

R2(config-if)#ip address 192.168.12.2 255.255.255.0 R2(config-if)#clock rate 128000 R1(config)#ip classless

R2(config)#ip route 10.1.1.0 255.255.255.0 Serial0/0/0

测试从R1 ping R2的loopback0接口 R1#ping 10.2.2.2

Type escape sequence to abort.

Sending 5, 100-byte ICMP Echos to 10.2.2.2, timeout is 2 seconds: !!!!!

Success rate is 100 percent (5/5), round-trip min/avg/max = 12/13/16 ms

可以ping通。

(2) 步骤2:执行“no ip classless” R1(config)#no ip cef

//关闭ip cef,防止影响我们的测试,CEF R1(config)#no ip classless

R1#ping 10.2.2.2

Type escape sequence to abort.

Sending 5, 100-byte ICMP Echos to 10.2.2.2, timeout is 2 seconds: .....

Success rate is 0 percent (0/5)

可以看到R1虽然存在默认路由,也不能可以ping通R2的loopback0接口。

3.5 本章小结

本章介绍了静态路由的配置方法,命令虽然简单,但是静态路由的配置却有一定的技巧性。我们也介绍了路由表的查看方法以及路由表中各字段的含义。默认路由常常用于桩网络中,处于局域网和Internet边界的路由器通常会使用它。表3-2是本章出现的命令。

表3-2 本章命令汇总

命令

ip route show ip route

ip classless/ no ip classless ping 2.2.2.2 source loopback 0

配置静态路由 查看路由表

打开/关闭有类路由功能 指定源端口进行ping测试

作用

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo0.com 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务