一、什么是ospf协议
Open Shortest Path First开放式最短路径优先。配置了ospf协议的路由器,会将自己知道的接口的带宽、开销、ip等信息共享给其它路由器,最终每个路由器都知道整个网络的拓扑。每个路由器再通过SPF算法,建立一个以自己为根节点的SPF树,再依据SPF树形成路由表。
二、IGP(内部网关协议)分类
距离矢量:rip、eigrp
链路状态:ospf
距离矢量协议中任何一台路由器只知道跟邻居是通过哪个接口连的,不知道邻居跟其他路由器是通过哪个接口连接,邻居只会告知这台路由器自己知道的路由。
链路状态协议中任何一台路由器知道整个网络拓扑图,知道其他任意路由器的接口是怎么连接、接口的带宽、开销、ip等信息。而且每个路由器中的路由表信息是本地cpu根据拓扑图计算的最优路径得到的。
三、ospf区域概念
如果网络中的路由器越来越多,数据库就越大,cpu计算就会变慢,因此产生了区域的概念,区域使用数字表,划分区域后,每个区域中的设备只能知道自己区域的拓扑图。区域之间使用距离矢量协议将计算的路由共享给对方
由于区域之间使用了距离矢量协议,可能会发生环路,为避免环路区域使用星型结构,只允许骨干和非骨干区域传递路由,非骨干区域之间不允许传递路由信息。
注意:一个路由器的不同接口可以属于不同区域,但是同一根线上的接口要属于同一个区域
四、实验配置ospf协议
R1:f0/0 10.1.12.1/24 f0/1 10.1.13.1/24
R2:f0/0 10.1.12.2/24 f0/1 10.1.24.2/24 l0:2.2.2.2/24
R3:f0/0 10.1.13.3/24 f0/1 10.1.34.3/24 l0:3.3.3.3/24
R4:f0/0 10.1.24.4/24 f0/1:10.1.34.4/24

以R2为例,所有接口开启ospf
R2(config)#router ospf 1 #1表示进程id,每个路由器上进程id不需要一致
R2(config-router)#net 10.0.0.0 0.255.255.255 area 0 #区域id必须一致,这里只做单区域测试,不一致无法共享信息
R2(config-router)#net 2.2.2.2 0.0.0.0 area 0
注意:
1、ospf没有关闭自动汇总命令,因为路由器邻居间传输的不是路由,而是接口是怎么连接、接口的带宽、开销、ip等信息。
2、router-id表示路由器的名称,格式类似ip。如果两个设备命名重复会导致邻居关系无法建立。这里没有明确指定router-id,系统会自动从接口中选出一个ip作为名称,规则是:环回口优先,选出ip最大的,没有环回口就从物理口中选出ip最大的。
R2(config-router)#router-id ?
A.B.C.D OSPF router-id in IP address format
查看R2路由表信息
2.0.0.0/24 is subnetted, 1 subnets
C 2.2.2.0 is directly connected, Loopback0
3.0.0.0/32 is subnetted, 1 subnets
O 3.3.3.3 [110/21] via 10.1.24.4, 00:00:01, FastEthernet0/1
[110/21] via 10.1.12.1, 00:00:01, FastEthernet0/0
10.0.0.0/24 is subnetted, 4 subnets
O 10.1.13.0 [110/20] via 10.1.12.1, 00:01:58, FastEthernet0/0
C 10.1.12.0 is directly connected, FastEthernet0/0
C 10.1.24.0 is directly connected, FastEthernet0/1
O 10.1.34.0 [110/20] via 10.1.24.4, 00:01:50, FastEthernet0/1
3.0.0.0/24这个环回口在R3上查看路由表是24位,这里变成了32位,具体原因暂时先不深究,只要在环回口下:ip ospf network point-to-point,就正常了。
开销计算示例
R2到3.0.0.0/24这条路由是一个负载均衡,
O 3.3.3.3 [110/21] via 10.1.24.4, 00:00:01, FastEthernet0/1
[110/21] via 10.1.12.1, 00:00:01, FastEthernet0/0
以下一跳是10.1.24.4为例,计算开销

为了计算开销方便,从逻辑上把3.0.0.0这条路由看做是从R3传递过来的。其实是R2本地自己计算的。计算方式是每个入接口10^8/接口带宽(单位bps)的值(小于1算1)的和。
R3#show int l0
BW 8000000 Kbit/sec
R1#show int f0/1
BW 10000 Kbit/sec
R2# show int f0/0
BW 10000 Kbit/sec
计算结果是21
修改开销值示例
O 3.3.3.3 [110/21] via 10.1.24.4, 00:00:01, FastEthernet0/1
[110/21] via 10.1.12.1, 00:00:01, FastEthernet0/0
到3.0.0.0这个网段走的负载均衡,如果想通过R1过去,需要修改入接口的cost值,修改f0/1开销值变小,这样就会优先从R1走。
(1)修改R1 f0/1带宽,从而减小开销
R1#show int f0/1 //目前是10Mb
--->BW 10000 Kbit/sec
R1#conf terminal
R1(config)#int f0/1
R1(config-if)#bandwidth 100000 //修改为100Mb
R1#show int f0/1 //已经修改为100Mb
FastEthernet0/1 is up, line protocol is up
MTU 1500 bytes, BW 100000 Kbit/sec, DLY 1000 usec,
查看修改后的接口开销,已经从10变成了1
BW 100000 Kbit/sec, DLY 1000 usec。查看接口看到的是参考带宽和参考延时都,是逻辑值,不是真正的物理带宽和延时,只是协议计算开销使用,修改后对物理值无影响。
R1#show ip ospf interface brief
Interface PID Area IP Address/Mask Cost State Nbrs F/C
Fa0/1 1 0 10.1.13.1/24 1 DR 1/1
Fa0/0 1 0 10.1.12.1/24 10 DR 1/1
再查看R2的路由表,已经达到预期效果:
3.0.0.0/32 is subnetted, 1 subnets
O 3.3.3.3 [110/12] via 10.1.12.1, 00:03:07, FastEthernet0/0
(2)直接修改R1 f0/1接口的开销值(推荐,防止修改参考带宽对其他协议计算开销有影响)
R1(config)#int f0/1
R1(config-if)#ip ospf cost 9
五、查看ospf协议的四张表
1、接口表
//加上brief命令可以看到精简信息
R2#show ip ospf interface brief
Interface PID Area IP Address/Mask Cost State Nbrs F/C
Fa0/1 1 0 10.1.24.2/24 10 BDR 1/1
Fa0/0 1 0 10.1.12.2/24 10 BDR 1/1
Lo0 1 0 2.2.2.2/24 1 LOOP 0/0
2、邻居表
R2#show ip ospf neighbor
Neighbor ID Pri State Dead Time Address Interface
10.1.34.4 1 FULL/DR 00:00:31 10.1.24.4 FastEthernet0/1
10.1.13.1 1 FULL/DR 00:00:31 10.1.12.1 FastEthernet0/0
Neighbor ID:邻居的路由器名称,不是ip
State:FULL表示网络正常,其他状态都是异常
Dead Time:跟eigrp类似,ospf是每隔10秒发送一次hello包,连续4次没有收到说明对方网络断开。
Address:邻居接口的ip地址
Interface:跟自己的哪个接口相连
3、数据库表
R2#show ip ospf database
OSPF Router with ID (2.2.2.2) (Process ID 1)
Router Link States (Area 0)
Link ID ADV Router Age Seq# Checksum Link count
2.2.2.2 2.2.2.2 67 0x80000003 0x009BCC 3
3.3.3.3 3.3.3.3 69 0x80000003 0x00053F 3
10.1.13.1 10.1.13.1 73 0x80000002 0x004E2D 2
10.1.34.4 10.1.34.4 71 0x80000003 0x007388 2
Net Link States (Area 0)
Link ID ADV Router Age Seq# Checksum
10.1.12.1 10.1.13.1 73 0x80000001 0x00F0F6
10.1.13.1 10.1.13.1 73 0x80000001 0x0018CA
10.1.24.4 10.1.34.4 71 0x80000001 0x008424
10.1.34.4 10.1.34.4 71 0x80000001 0x004852
Router Link States (Area 0)
这里面的条目是每个路由器把自己知道的接口是怎么连接、接口的带宽、开销、ip等信息打包共享给邻居。R2#show ip ospf database router 2.2.2.2
可以查看共享给邻居的具体信息。
4、路由表
数据库中的信息通过SPF算法得到的路由放到路由表中
R2#show ip route
2.0.0.0/24 is subnetted, 1 subnets
C 2.2.2.0 is directly connected, Loopback0
3.0.0.0/32 is subnetted, 1 subnets
O 3.3.3.3 [110/21] via 10.1.24.4, 00:10:34, FastEthernet0/1
[110/21] via 10.1.12.1, 00:10:44, FastEthernet0/0
10.0.0.0/24 is subnetted, 4 subnets
O 10.1.13.0 [110/20] via 10.1.12.1, 00:10:44, FastEthernet0/0
C 10.1.12.0 is directly connected, FastEthernet0/0
C 10.1.24.0 is directly connected, FastEthernet0/1
O 10.1.34.0 [110/20] via 10.1.24.4, 00:10:36, FastEthernet0/1
六、ospf路由认证
1、明文认证
R2 f/0和R1 f0/0
R2(config)#int f0/0
R2(config-if)#ip ospf authentication ? //<cr>表示回车,这里回车就表示明文认证
message-digest Use message-digest authentication
null Use no authentication
<cr>
R2(config-if)#ip ospf authentication-key 1 cisco //没有钥匙串,配置钥匙id和密码即可
R1(config)#int f0/0
R1(config-if)#ip ospf authentication
R1(config-if)#ip ospf authentication-key 3 cisco // key id可以不一致,密码必须一致
2、md5密文认证
R2#conf terminal
R2(config)#int f0/0
R2(config-if)#ip ospf authentication message-digest
R2(config-if)#ip ospf message-digest-key 1 md5 cisco
R2(config-if)#end
R1#conf terminal
R1(config)#int f0/0
R1(config-if)#ip ospf authentication message-digest
R1(config-if)#ip ospf message-digest-key 1 md5 cisco //key id和密码必须都一致
R1(config-if)#end
查看指定接口的所有配置信息
R2#show running-config int f0/0
Building configuration...
Current configuration : 173 bytes
!
interface FastEthernet0/0
ip address 10.1.12.2 255.255.255.0
ip ospf authentication message-digest
ip ospf message-digest-key 1 md5 cisco
duplex auto
speed auto
end
网友评论