一、OSPF概述
 
      OSPF的全称是Open Shortest Path First ,译为开放最短路径优先。OSPF是一种典型的链路状态型路由协议,是从IS-IS的早期版本发展而来。OSPF一般用于同一个路由域内。路由域是指一个自治系统(Autonomous System),即AS,它是指一组通过统一的路由策略或路由协议互相交换路由信息的网络。在这个AS中,所有的OSPF路由器都维护一个相同的描述这个AS结构的数据库,该数据库中存放的是路由域中相应链路的状态信息,运行OSPF路由选择协议的路由器正是通过这个数据库计算出其OSPF路由表的。作为一种链路状态型路由协议,OSPF将链路状态广播数据包LSA(Link State Advertisement)传送给在某一区域内的所有路由器,这一点与距离矢量路由协议不同。运行距离矢量路由协议的路由器是将部分或全部的路由表传递给与其相邻的路由器。

二、在Cisco路由器上实现OSPF
 
1、网络拓扑:
 
 
2、R1的配置如下:
 
Router(config)#hostname R1
R1(config)#interface loopback 0
R1(config-if)#ip address 1.1.1.1 255.255.255.0
R1(config-if)#ip ospf network point-to-point
R1(config-if)#exit
R1(config)#interface serial 0/0
R1(config-if)#ip address 10.0.0.1 255.255.255.252
R1(config-if)#clock rate 64000
R1(config-if)#no shutdown
R1(config-if)#exit
R1(config)#interface serial 0/1
R1(config-if)#ip address 40.0.0.2 255.255.255.252
R1(config-if)#no shutdown
R1(config-if)#exit
R1(config)#router ospf 1
R1(config-router)#network 1.1.1.0 0.0.0.255 area 0
R1(config-router)#network 10.0.0.0 0.0.0.3 area 0
R1(config-router)#network 40.0.0.0 0.0.0.3 area 0
R1(config-router)#exit
 
3、R2的配置如下:
 
Router(config)#hostname R2
R2(config)#interface loopback 0
R2(config-if)#ip address 2.2.2.2 255.255.255.0
R2(config-if)#ip ospf network point-to-point
R2(config-if)#no shutdown
R2(config-if)#exit
R2(config)#interface serial 0/0
R2(config-if)#ip address 10.0.0.2 255.255.255.252
R2(config-if)#no shutdown
R2(config-if)#exit
R2(config)#interface serial 0/1
R2(config-if)#ip address 20.0.0.1 255.255.255.252
R2(config-if)#clock rate 64000
R2(config-if)#no shutdown
R2(config-if)#exit
R2(config)#router ospf 1
R2(config-router)#network 2.2.2.0 0.0.0.255 area 0
R2(config-router)#network 10.0.0.0 0.0.0.3 area 0
R2(config-router)#network 20.0.0.0 0.0.0.3 area 0
R2(config-router)#exit
 
4、R3的配置如下:
 
Router(config)#hostname R3
R3(config)#interface loopback 0
R3(config-if)#ip address 3.3.3.3 255.255.255.0
R3(config-if)#ip ospf network point-to-point

R3(config-if)#exit
R3(config)#interface serial 0/0
R3(config-if)#ip address 20.0.0.2 255.255.255.252
R3(config-if)#no shutdown
R3(config-if)#exit
R3(config)#interface serial 0/1
R3(config-if)#ip address 30.0.0.1 255.255.255.252
R3(config-if)#clock rate 64000
R3(config-if)#no shutdown
R3(config-if)#exit
R3(config)#router ospf 1
R3(config-router)#network 3.3.3.0 0.0.0.255 area 0
R3(config-router)#network 20.0.0.0 0.0.0.3 area 0
R3(config-router)#network 30.0.0.0 0.0.0.3 area 0
R3(config-router)#exit
 
5、R4的配置如下:
 
Router(config)#hostname R4
R4(config)#interface loopback 0
R4(config-if)#ip address 4.4.4.4 255.255.255.0
R4(config-if)#ip ospf network point-to-point
R4(config-if)#exit
R4(config)#interface s0/0
R4(config-if)#ip address 30.0.0.2 255.255.255.252
R4(config-if)#no shutdown
R4(config-if)#exit
R4(config)#interface serial 0/1
R4(config-if)#ip address 40.0.0.1 255.255.255.252
R4(config-if)#clock rate 64000
R4(config-if)#no shutdown
R4(config-if)#exit
R4(config)#router ospf 1
R4(config-router)#network 4.4.4.0 0.0.0.255 area 0
R4(config-router)#network 30.0.0.0 0.0.0.3 area 0
R4(config-router)#network 40.0.0.0 0.0.0.3 area 0
R4(config-router)#exit
 
6、验证OSPF的配置:
 
R1:
 
R1#show ip protocols  ---查看路由器的IP路由选择协议参数。
Routing Protocol is "ospf 1"
  Sending updates every 90 seconds, next due in 10 seconds
  Invalid after 30 seconds, hold down 0, flushed after 60
  Outgoing update filter list for all interfaces is
  Incoming update filter list for all interfaces is
  Redistributing: ospf 1
  Routing for Networks:
    1.1.1.0 0.0.0.255 area 0
    10.0.0.0 0.0.0.255 area 0
    40.0.0.0 0.0.0.255 area 0
  Routing Information Sources:
    Gateway         Distance      Last Update
    1.1.1.1              110      00:00:03
    10.0.0.1             110      00:00:03
    10.0.0.1             110      00:00:03
  Distance: (default is 110)
 
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
       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, * - candidate default
       U - per-user static route
Gateway of last resort is not set
     1.0.0.0/24 is subnetted, 1 subnets
C       1.1.1.0 is directly connected, Loopback0
     10.0.0.0/24 is subnetted, 1 subnets
C       10.0.0.0 is directly connected, Serial0/0
     40.0.0.0/24 is subnetted, 1 subnets
C       40.0.0.0 is directly connected, Serial0/1
     2.0.0.0/24 is subnetted, 1 subnets
O       2.2.2.0 [110/64] via 10.0.0.2, 00:13:01, Serial0/0
     20.0.0.0/24 is subnetted, 1 subnets
O       20.0.0.0 [110/64] via 20.0.0.1, 00:12:46, Serial0/0
     3.0.0.0/24 is subnetted, 1 subnets
O       3.3.3.0 [110/192] via 10.0.0.2, 00:11:55, Serial0/0
     30.0.0.0/24 is subnetted, 1 subnets
O       30.0.0.0 [110/192] via 10.0.0.2, 00:11:45, Serial0/0
     4.0.0.0/24 is subnetted, 1 subnets
O       4.4.4.0 [110/320] via 10.0.0.2, 00:10:05, Serial0/0
 
R1#show ip ospf neighbor detail   ---查看邻居的详细信息。
 Neighbor 2.2.2.2, interface address 10.0.0.2
    In the area 1 via interface Serial0/0
    Neighbor priority is 1, State is FULL, 6 state changes
    DR is 0.0.0.0 BDR is 0.0.0.0
    Options 2
    Dead timer due in
 Neighbor 4.4.4.4, interface address 40.0.0.1
    In the area 1 via interface Serial0/1
    Neighbor priority is 1, State is FULL, 6 state changes
    DR is 0.0.0.0 BDR is 0.0.0.0
    Options 2
    Dead timer due in
 
R2:
 
R2#show ip protocols
Routing Protocol is "ospf 1"
  Sending updates every 90 seconds, next due in 10 seconds
  Invalid after 30 seconds, hold down 0, flushed after 60
  Outgoing update filter list for all interfaces is
  Incoming update filter list for all interfaces is
  Redistributing: ospf 1
  Routing for Networks:
    2.2.2.0 0.0.0.255 area 0
    10.0.0.0 0.0.0.255 area 0
    20.0.0.0 0.0.0.255 area 0
  Routing Information Sources:
    Gateway         Distance      Last Update
    2.2.2.2              110      00:00:03
    2.2.2.2              110      00:00:03
    10.0.0.2             110      00:00:03
    10.0.0.2             110      00:00:03
    20.0.0.1             110      00:00:03
  Distance: (default is 110)
 
R2#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
       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, * - candidate default
       U - per-user static route
Gateway of last resort is not set
     2.0.0.0/24 is subnetted, 1 subnets
C       2.2.2.0 is directly connected, Loopback0
     10.0.0.0/24 is subnetted, 1 subnets
C       10.0.0.0 is directly connected, Serial0/0
     20.0.0.0/24 is subnetted, 1 subnets
C       20.0.0.0 is directly connected, Serial0/1
     1.0.0.0/24 is subnetted, 1 subnets
O       1.1.1.0 [110/64] via 10.0.0.1, 00:26:27, Serial0/0
     40.0.0.0/24 is subnetted, 1 subnets
O       40.0.0.0 [110/64] via 40.0.0.2, 00:26:37, Serial0/0
     3.0.0.0/24 is subnetted, 1 subnets
O       3.3.3.0 [110/64] via 20.0.0.2, 00:24:21, Serial0/1
     30.0.0.0/24 is subnetted, 1 subnets
O       30.0.0.0 [110/64] via 30.0.0.1, 00:24:11, Serial0/1
     4.0.0.0/24 is subnetted, 1 subnets
O       4.4.4.0 [110/192] via 20.0.0.2, 00:24:31, Serial0/1
 
R2#show ip ospf neighbor detail
 Neighbor 1.1.1.1, interface address 10.0.0.1
    In the area 1 via interface Serial0/0
    Neighbor priority is 1, State is FULL, 6 state changes
    DR is 0.0.0.0 BDR is 0.0.0.0
    Options 2
    Dead timer due in
 Neighbor 3.3.3.3, interface address 20.0.0.2
    In the area 1 via interface Serial0/1
    Neighbor priority is 1, State is FULL, 6 state changes
    DR is 0.0.0.0 BDR is 0.0.0.0
    Options 2
    Dead timer due in
 
R3:
 
R3#show ip protocols
Routing Protocol is "ospf 1"
  Sending updates every 90 seconds, next due in 10 seconds
  Invalid after 30 seconds, hold down 0, flushed after 60
  Outgoing update filter list for all interfaces is
  Incoming update filter list for all interfaces is
  Redistributing: ospf 1
  Routing for Networks:
    3.3.3.0 0.0.0.255 area 0
    20.0.0.0 0.0.0.255 area 0
    30.0.0.0 0.0.0.255 area 0
  Routing Information Sources:
    Gateway         Distance      Last Update
    3.3.3.3              110      00:00:03
    3.3.3.3              110      00:00:03
    20.0.0.2             110      00:00:03
    20.0.0.2             110      00:00:03
  Distance: (default is 110)
 
R3#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
       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, * - candidate default
       U - per-user static route
Gateway of last resort is not set
     3.0.0.0/24 is subnetted, 1 subnets
C       3.3.3.0 is directly connected, Loopback0
     20.0.0.0/24 is subnetted, 1 subnets
C       20.0.0.0 is directly connected, Serial0/0
     30.0.0.0/24 is subnetted, 1 subnets
C       30.0.0.0 is directly connected, Serial0/1
     10.0.0.0/24 is subnetted, 1 subnets
O       10.0.0.0 [110/128] via 20.0.0.1, 00:30:35, Serial0/0
     2.0.0.0/24 is subnetted, 1 subnets
O       2.2.2.0 [110/64] via 20.0.0.1, 00:30:35, Serial0/0
     40.0.0.0/24 is subnetted, 1 subnets
O       40.0.0.0 [110/128] via 30.0.0.2, 00:29:45, Serial0/1
     4.0.0.0/24 is subnetted, 1 subnets
O       4.4.4.0 [110/64] via 30.0.0.2, 00:29:45, Serial0/1
 
R3#show ip ospf neighbor detail
 Neighbor 2.2.2.2, interface address 20.0.0.1
    In the area 1 via interface Serial0/0
    Neighbor priority is 1, State is FULL, 6 state changes
    DR is 0.0.0.0 BDR is 0.0.0.0
    Options 2
    Dead timer due in
 Neighbor 4.4.4.4, interface address 30.0.0.2
    In the area 1 via interface Serial0/1
    Neighbor priority is 1, State is FULL, 6 state changes
    DR is 0.0.0.0 BDR is 0.0.0.0
    Options 2
    Dead timer due in
 
R4:
 
R4#show ip protocols
Routing Protocol is "ospf 1"
  Sending updates every 90 seconds, next due in 10 seconds
  Invalid after 30 seconds, hold down 0, flushed after 60
  Outgoing update filter list for all interfaces is
  Incoming update filter list for all interfaces is
  Redistributing: ospf 1
  Routing for Networks:
    4.4.4.0 0.0.0.255 area 0
    30.0.0.0 0.0.0.255 area 0
    40.0.0.0 0.0.0.255 area 0
  Routing Information Sources:
    Gateway         Distance      Last Update
    4.4.4.4              110      00:00:03
    4.4.4.4              110      00:00:03
    30.0.0.2             110      00:00:03
    30.0.0.2             110      00:00:03
  Distance: (default is 110)
 
R4#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
       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, * - candidate default
       U - per-user static route
Gateway of last resort is not set
     4.0.0.0/24 is subnetted, 1 subnets
C       4.4.4.0 is directly connected, Loopback0
     30.0.0.0/24 is subnetted, 1 subnets
C       30.0.0.0 is directly connected, Serial0/0
     40.0.0.0/24 is subnetted, 1 subnets
C       40.0.0.0 is directly connected, Serial0/1
     20.0.0.0/24 is subnetted, 1 subnets
O       20.0.0.0 [110/128] via 30.0.0.1, 00:32:17, Serial0/0
     3.0.0.0/24 is subnetted, 1 subnets
O       3.3.3.0 [110/64] via 30.0.0.1, 00:32:17, Serial0/0
     10.0.0.0/24 is subnetted, 1 subnets
O       10.0.0.0 [110/128] via 40.0.0.2, 00:32:07, Serial0/1
     1.0.0.0/24 is subnetted, 1 subnets
O       1.1.1.0 [110/64] via 40.0.0.2, 00:32:07, Serial0/1

R4#show ip ospf neighbor detail
 Neighbor 1.1.1.1, interface address 40.0.0.2
    In the area 1 via interface Serial0/1
    Neighbor priority is 1, State is FULL, 6 state changes
    DR is 0.0.0.0 BDR is 0.0.0.0
    Options 2
    Dead timer due in
 Neighbor 3.3.3.3, interface address 30.0.0.1
    In the area 1 via interface Serial0/0
    Neighbor priority is 1, State is FULL, 6 state changes
    DR is 0.0.0.0 BDR is 0.0.0.0
    Options 2
    Dead timer due in