9.静态路由协议的配置,带下一跳地址和带出接口的协议配置,检查和排错
9.1静态路由介绍
路由器在转发数据时,要先在路由表(routing table)中查找相应的路由。路由器有这么三种途径建立路由:
(1) 直连网络:路由器自动添加和自己直接连接的网络的路由
(2) 静态路由:管理员手动输入到路由器的路由
(3) 动态路由:由路由协议(routing protocol)动态建立的路由
静态路由的缺点是不能动态反映网络拓扑,当网络拓扑发生变化时,管理员就必须手工改变路由表;然而静态路不会占用路由器太多的CPU 和RAM 资源,也不占用线路的带宽。如果出于安全的考虑想隐藏网络的某些部分或者管理员想控制数据转发路径,也会使用静态路由。在一个小而简单的网络中,也常使用静态路由,因为配置静态路由会更为简捷。
配置静态路由的命令为“ip route”,命令的格式如下:
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.160.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”是指路由器最近一次得知路由到现在的时间;”Serials0/0”是指到达下一跳应从哪个端口出去。
【技术要点】
管理距离(AD);用来表示路由的可信度,路由器可能从多种途径获得同一路由,例如,一个路由器要获得”
10.2.0.0/24”网络的路由,可以来自RIP,可以是静态路由,不同途径获得的路由可能采取不同的路径到达目的网络,
为了区分不同路由协议的可信度,用管理距离加以表示。表3-1 是通过各种路由协议获得的路由的默认管理距离。路
由表中管理距离值越小,说明路由的可靠程度越高;静态路由的管理距离为1,说明手工输入的路由优先级高于其他的
路由。

路由协议的默认管理距离
路由协议 管理距离
直连接口 0
静态路由 0(出接口) 1(下一跳)
外部BGP 20
内部EIGRP 90
IGRP 100
OSPF 110
RIP 120
外部EIGRP 170
汇总EIGRP 5

                      静态路由配置

实验拓扑:(由于图片问题没有即使显示,抱歉,之后会改进)
 R1、R2、R3以太网相连,R1、R2间是E0/0口相连,网段192.168.12.0/24,R2、R3间 E0/1口相连,网段192.168.23.0/24,R1;o0:1.1.1.1/32,R2lo0:2.2.2.2/32,R3lo0:3.3.3.3/32


实验目标:
熟练掌握静态路由的配置
在全局配置模式下:配下一跳地址
R1(config)#ip router 2.2.2.0 255.255.255.0 192.168.12.2
R1(config)#ip router 3.3.3.0 255.255.255.0 192.168.12.2

R2(config)#ip route 1.1.1.0 255.255.255.0 192.168.12.1
R2(config)#ip route 3.3.3.0 255.255.255.0 192.168.23.3

R3(config)#ip route 1.1.1.0 255.255.255.0 192.168.23.2
R3(config)#ip route 2.2.2.0 255.255.255.0 192.168.23.2

删除操作:
router1(config)#no ip router 《》《》《》

配出接口:(点到点链路)
router1(config)#ip router 10.10.23.0 255.255.255.0 s0/0
router3(config)#ip router 10.10.12.0 255.255.255.0 s0/1

查看路由表:
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, Ethernet0/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 [1/0] via 192.168.12.2
     3.0.0.0/24 is subnetted, 1 subnets
S       3.3.3.0 [1/0] via 192.168.12.2
R1#
注:S 代表静态路由、管理距离值为1、度量值为0。
注:除了可以用下一跳地址,还可以用出站接口的名称。
注:使用本地出站接口的静态路由,它就好像是直连在路由器的接口上的,所以它的管理距
离值为0

R2#show ip route
(省略)
C    192.168.12.0/24 is directly connected, Ethernet0/0
     1.0.0.0/24 is subnetted, 1 subnets
S       1.1.1.0 [1/0] via 192.168.12.1
     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 [1/0] via 192.168.23.3
C    192.168.23.0/24 is directly connected, Ethernet0/1
R2#


R3#show ip route
(省略)

     1.0.0.0/24 is subnetted, 1 subnets
S       1.1.1.0 [1/0] via 192.168.23.2
     2.0.0.0/24 is subnetted, 1 subnets
S       2.2.2.0 [1/0] via 192.168.23.2
     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, Ethernet0/1
R3#
R3#

从各路由器的环回口ping 其他路由器的环回口
R1#ping
//不带任何参数的ping 命令,允许输入更多的参数
Protocol [ip];
Target IP address; 2.2.2.2 //目标IP 地址
Repeat count [5]; //发送的ping 次数
Datagram size [100]; //ping 包的大小
Timeout in seconds [2]; //超时时间
Extended commands [n]; y //是否进一步扩展命令
Source address or interface; 1.1.1.1 //源IP 地址
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
//以上说明从R1 的loopback 0 可以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 的loopback 0 应该可以ping 通R1 和R3 的loopback 9 接口
R3#ping 1.1.1.1 source loopback 0
R3#ping 2.2.2.2 source loopback 0
从R3 的loopback 0 也应该可以ping 通R1 和R2 的loopback 9 接口
【提示】
虽然从R1 的loopback0 可以ping 通R3 的loopback 0,数据需要以过192.168.23.0/24 网络,但是在R1 上,我们
并没有添加192.168.23.0/24 的路由。路由器转发数据包完全是根据路由表进行的,并且数据是一跳一跳地被转发的,
就像接力赛似的。当从R1 的loopbak 0 口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.1.2.1)
作为IP 数据包的源IP 地址。当R3 上响应R1 的数据包时,数据包是发向192.168.12.1 的。然而,由于R3 没有
192.168.12.0/24 的路由,数据包无法发送。即:数据包人R1 到了R3 后,无法返回R1 。


9.2 默认路由介绍
所谓的默认路由,是指路由器在路由表中如果找不到到达目的网络的具体路由时,最后会采用的路由。默认路由通常会在存根网络(Stub network,即只有一个出口的网络)中使用。
配置默认路由。命令为: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
默认路由在本地路由表中是s*表示的!
9.2.1 ip classless
如果在R1 上配置了默认路由:ip route 0.0.0.0 0.0.0.0 s0/0/0,则R1路由器是否会把到达10.10.23.0/24 网络的数据从s0/0/0 接口发送出去?这取决于是否执行了“ip classless”命令。如果执行了“ip classless”命令(实际上这是默认值),则路由器存在默认路由时,所有在路由表中查不到具体路由的数据包将通过默认路由发送。
如果执行了“no ip classless”命令,当路由器存在一主类网络的某一子网路由时,路由器将认为自己已经知道该主类网络的全部子网的路由,这时即使存在默认路由,到达该主类任一子网的数据包不会通过默认路由发送。图3-2 中,执行了“no ip classless”后,由于R1 路由器上有10.0.0.0 的子网10.10.12.0/24(这是直连路由),因此R1 路由器收到到达10.10.23.0/24 子网的数据包不会使用默认路由进行发送。然而如果数据包是要到达20.20.23.0/24,默认路由会被采用,因为R1 没有任何20.0.0.0 子网的路由。

                  默认路由配置

实验步骤
在实验1 的基础上进行实验2
(1)步骤1:在R1 和R3 上删除原有静态路由
R1(config)#no ip route 2.2.2.0 255.255.255.0 s0/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 s0/0/1
R3(config)#no ip route 2.2.2.0 255.255.255.0 s0/0/1
(2)步骤2:在R1 和R3 上配置默认路由
R1(config)#ip route 0.0.0.0 0.0.0.0 192.168.12.2
R3(config)#ip route 0.0.0.0 0.0.0.0 192.168.23.2
实验调试
从各路由器的环回口ping 其他路由器的环回口。请读者比较两个实验ping 的结果,仔细分析原因。
各回环口之间不能ping通(不可达),原因是R2路由器不知道怎么去往目标,如果R2配置两条默认路由也不能ping通,当配置两条静态路由就可以ping通!

实验3:ip classless
1.实验目的
通过本实验,读者可以掌握如下技能;
① ip classless 命令的含义;
② 配置 ip classless
2.实验拓扑
实验拓扑图如图所示。

  R1、R2以太网相连,R1、R2间是E0/0口相连,网段192.168.12.0/24,网段192.168.23.0/24,R1;o0:1.1.1.1/32,R2lo0:2.2.2.2/32

3.实验步骤
(1)步骤1:执行”ip classless”
R1(config)#interface loopback0
R1(config-if)#ip address 1.1.1.1 255.255.255.0
R1(config)#interface e0/0
R1(config-if)#ip address 192.168.12.1 255.255.255.0
R1(config-if)#no shutdown
R1(config)#ip classless
R1(config)#ip route 0.0.0.0 0.0.0.0 192.168.12.2
//以上我们配置了默认路由,同时打开”ip classless”,默认就是打开的
R2(config)#interface loopback0
R2(config-if)#ip address 1.1.2.1 255.255.255.0
R2(config)#interface e0/0
R2(config-if)#no shutdown
R2(config-if)#ip address 192.168.12.2 255.255.255.0
R2(config)#ip classless
R2(config)#ip route 10.1.1.0 255.255.255.0 Serial0/0/0
//测试从R1 ping R2 的loopback 0 接口
R1#ping 1.1.2.1
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 1.1.2.1, timeout is 2 seconds;
!!!!!
Success rate is 100 percent (5/5),round-trip min/avg/max = 12/14/16 ms
//可以ping 通
(2)步骤2:执行”no ip classless”
R1(config)#no ip cef(必须做!)
//关闭ip cef,防止影响我们测试
R1(config)#no ip classless
R1#ping 1.1.2.1
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 1.1.2.1, timeout is 2 seconds;
.....
Success rate is 0 percent (0/5)
可以看到,R1 虽然存在默认路由,也不能ping 通R2 的loopback 0 接口