首先综述一下ospf相对于其他内部网关协议的优点:ospf是一种链路状态协议,收敛速度快,占用资源较少,支持区域划分,通过区域的划分分为骨干区域和非骨干区域,可避免路由环路的产生,非骨干区域只和骨干区域进行通讯建立邻居,这样既可以减少路由器的资源浪费亦可以避免路由环路,最重要的是ospf支持大型网络,不像rip最大支持16跳,把网络限制在15台路由器内,在网络的扩展上有一定的局限性

再者来说说ospf的路由度量,相对于距离矢量协议,ospf采用最短路径优先算法,可避免在rip中出现的选取次佳路径的状况.

最后简述一下ospf的路由建立过程,ospf采用触发更新,通过hello包来建立邻居,建立邻居后,相互更新链路状态数据库,然后自己通过spf算法算出最佳路径,建立路由表,同时也可以通过建立末节区域,和完全末节区域来减少路由条目的不断增多

下面通过一个案例来具提了解ospf的用法:

以下配置中rip网络中只能学到默认路由,以免ospf的庞大路由表项给rip网络中的路由器增加负担,而area 2配置了完全末端区域,使得R4只能学到区域内路由,但是同样可以给其他网络中的而计算机通讯,这样做可以减少资源浪费和路由条目不断增多时给路由器带来的负担

wps_clip_p_w_picpath-23164

R1

Router>en

Router#conf t

Enter configuration commands, one per line.  End with CNTL/Z.

Router(config)#host

Router(config)#hostname R1

R1(config)#int fa 0/0

R1(config-if)#ip add 192.168.1.1 255.255.255.0

R1(config-if)#no shu

R1(config-if)#

R1(config-if)#int s1/0

R1(config-if)#ip add 192.168.2.1 255.255.255.0

R1(config-if)#no shuR1(config)#router ospf 10

R1(config-router)#net

R1(config-router)#network

00:03:11: %LINEPROTO-5-UPDOWN: Line protocol on Interface Serial1/0, changed state to down192.168.1.0

R1(config-router)#network 192.168.1.0 0.0.0.255 area 1

R1(config-router)#network 192.168.2.0 0.0.0.255 area 1

R1(config-router)#

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, 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

O E2 192.168.8.0/24 [110/20] via 192.168.2.2, 00:07:07, Serial1/0

O IA 192.168.4.0/24 [110/192] via 192.168.2.2, 00:07:07, Serial1/0

O IA 192.168.5.0/24 [110/193] via 192.168.2.2, 00:01:51, Serial1/0

O E2 192.168.6.0/24 [110/20] via 192.168.2.2, 00:07:07, Serial1/0

O E2 192.168.7.0/24 [110/20] via 192.168.2.2, 00:07:07, Serial1/0

C    192.168.1.0/24 is directly connected, FastEthernet0/0

C    192.168.2.0/24 is directly connected, Serial1/0

O IA 192.168.3.0/24 [110/128] via 192.168.2.2, 00:07:08, Serial1/0

R1#

R2

Router>

Router>en

Router#conf t

Enter configuration commands, one per line.  End with CNTL/Z.

Router(config)#host

Router(config)#hostname R2

R2(config)#int s1/0

R2(config-if)#ip add 192.168.2.1 255.255.255.0

R2(config-if)#no shu

R2(config-if)#int s1

00:05:20: %LINK-3-UPDOWN: Interface Serial1/0, changed state to up/1

00:05:21: %LINEPROTO-5-UPDOWN: Line protocol on Interface Serial1/0, changed state to up

R2(config-if)#ip add 192.168.eixt

                             ^

% Invalid input detected at '^' marker.

R2(config-if)#

R2(config-if)#exi

R2(config)#int s1/0

R2(config-if)#ip add 192.168.2.2 255.255.255.0

R2(config-if)#no shut

R2(config-if)#int s1/1

R2(config-if)#ip add 192.168.3.1 255.255.255.0

R2(config-if)#no shu

R2(config-if)#

00:07:19: %LINK-3-UPDOWN: Interface Serial1/1, changed state to up

00:07:20: %LINEPROTO-5-UPDOWN: Line protocol on Interface Serial1/1, changed state to up

R2(config-if)#

R2(config-if)#int s1/3

R2(config-if)#ip add 192.168.6.1 255.255.255.0

R2(config-if)#no shut

R2(config-if)#

00:07:54: %LINK-3-UPDOWN: Interface Serial1/3, changed state to up

00:07:55: %LINEPROTO-5-UPDOWN: Line protocol on Interface Serial1/3, changed state to up

R2(config-if)#

R2(config-if)#

R2(config-if)#exi

R2(config)#rout

R2(config)#router osp

R2(config)#router ospf 10

00:08:21: %LINEPROTO-5-UPDOWN: Line protocol on Interface Serial1/3, changed state to down8.

R2(config-router)#network 192.168.2.0 0.0.0.255 area 1

R2(config-router)#network 192.168.3.0 0.0.0.255 area 0

R2(config-router)#exi

R2(config)#rout

R2(config)#router rip

R2(config-router)#net

R2(config-router)#network 192.168.6.0

R2(config-router)#

R2(config-router)#redistribute rip su

R2(config-router)#redistribute rip subnets

R2(config-router)#

R2(config-router)#

R2(config-router)#

R2(config-router)#R2(config)#router rip

R2(config-router)#de

R2(config-router)#default-

R2(config-router)#default-i

R2(config-router)#default-information ?

  originate  Distribute a default route

R2(config-router)#default-information o

R2(config-router)#default-information originate

R2(config-router)#

R3

Router>en

Router#conf t

Enter configuration commands, one per line.  End with CNTL/Z.

Router(config)#hos

Router(config)#hostname R3

R3(config)#int s1/1

R3(config-if)#ip add 192.168.3.2 255.255.255.0

R3(config-if)#no shu

R3(config-if)#

00:10:07: %LINK-3-UPDOWN: Interface Serial1/1, changed state to up

R3(config-if)#

00:10:08: %LINEPROTO-5-UPDOWN: Line protocol on Interface Serial1/1, changed state to up

R3(config-if)#int s1/2

R3(config-if)#ip add 192.168.4.1 255.255.255.0

R3(config-if)#no shu

R3(config-if)#exi

R3(config)#rou

R3(config)#router os

R3(config)#router ospf

00:10:51: %LINEPROTO-5-UPDOWN: Line protocol on Interface Serial1/2, changed state to down10

R3(config-router)#net

R3(config-router)#network 192.168.3.0 0.0.0.255 area 0

R3(config-router)#network 192.168.4.0 0.0.0.255 area 2

R4

Router>en

Router#conf t

Enter configuration commands, one per line.  End with CNTL/Z.

Router(config)#host

Router(config)#hostname R4

R4(config)#int s1/2

R4(config-if)#ip add 192.168.4.2 255.255.255.0

R4(config-if)#int fa 0/0

R4(config-if)#ip add 192.168.5.1 255.255.255.0

R4(config-if)#no shu

R4(config-if)#hostname R4                    

00:12:31: %LINK-3-UPDOWN: Interface FastEthernet0/0, changed state to up

00:12:32: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed int s1/2                       

R4(config-if)#no shut

R4(config-if)#exi

00:12:41: %LINK-3-UPDOWN: Interface Serial1/2, changed state to up

R4(config)#

00:12:42: %LINEPROTO-5-UPDOWN: Line protocol on Interface Serial1/2, changed state to uprou

R4(config)#router osp

R4(config)#router ospf 10

R4(config-router)#net     

R4(config-router)#network 192.168.4.0 0.0.0.255 area 2

R4(config-router)#network 192.168.4.0 0.0.0.255 area 2

00:13:19: %OSPF-5-ADJCHG: Process 10, Nbr 192.168.4.1 on Serial1/2 from LOADING to FULL, Loading Done

R4(config-router)#network 192.168.5.0 0.0.0.255 area 2

R4(config-router)#end

R4#show

00:13:34: %SYS-5-CONFIG_I: Configured from console by consoleip rou

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

       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, 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.4.0/24 is directly connected, Serial1/2

C    192.168.5.0/24 is directly connected, FastEthernet0/0

O IA 192.168.1.0/24 [110/193] via 192.168.4.1, 00:00:05, Serial1/2

O IA 192.168.2.0/24 [110/192] via 192.168.4.1, 00:00:05, Serial1/2

O IA 192.168.3.0/24 [110/128] via 192.168.4.1, 00:00:05, Serial1/2

R4#

R4#show ip rou

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

       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, 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

O E2 192.168.8.0/24 [110/20] via 192.168.4.1, 00:00:02, Serial1/2

C    192.168.4.0/24 is directly connected, Serial1/2

C    192.168.5.0/24 is directly connected, FastEthernet0/0

O E2 192.168.6.0/24 [110/20] via 192.168.4.1, 00:00:02, Serial1/2

O E2 192.168.7.0/24 [110/20] via 192.168.4.1, 00:00:02, Serial1/2

O IA 192.168.1.0/24 [110/193] via 192.168.4.1, 00:09:35, Serial1/2

O IA 192.168.2.0/24 [110/192] via 192.168.4.1, 00:09:35, Serial1/2

O IA 192.168.3.0/24 [110/128] via 192.168.4.1, 00:09:35, Serial1/2

R4#R4(config)#rou

R4(config)#router os

R4(config)#router ospf 10

R4(config-router)#?

rnate paths

R4(config-router)#are

R4(config-router)#area 2 ?

  authentication  Enable authentication

  default-cost    Set the summary default-cost of a NSSA/stub area

  nssa            Specify a NSSA area

  range           Summarize routes matching address/mask (border routers only)

  stub            Specify a stub area

  virtual-link    Define a virtual link and its parameters

R4(config-router)#area 2 stud

R4(config-router)#area 2 st 

R4(config-router)#area 2 stub ?

  no-summary  Do not send summary LSA into stub area

  <cr>

R4(config-router)#area 2 stub no-

R4(config-router)#area 2 stub no-summary

R4(config-router)#

00:27:20: %OSPF-5-ADJCHG: Process 10, Nbr 192.168.4.1 on Serial1/2 from FULL to DOWN, Neighbor Down: Adjacency forced to reset

R4(config-router)#

00:28:09: %OSPF-5-ADJCHG: Process 10, Nbr 192.168.4.1 on Serial1/2 from LOADING to FULL, Loading Done

R4(config-router)#

R4(config-router)#

R4(config-router)#end

R4#sho wi

00:28:12: %SYS-5-CONFIG_I: Configured from console by console

R4#show ip rou

R4#show ip route

Gateway of last resort is not set

C    192.168.4.0/24 is directly connected, Serial1/2

C    192.168.5.0/24 is directly connected, FastEthernet0/0

R4#ping 192.168.8.1

Type escape sequence to abort.

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

!!!!!

Success rate is 100 percent (5/5), round-trip min/avg/max = 132/168/208 ms

R4#

R5

Router&gt;

Router&gt;

Router&gt;en

Router#conf t

Enter configuration commands, one per line.  End with CNTL/Z.

Router(config)#int s1/3

Router(config-if)#ip add 192.168.6.1 255.255.255.0

Router(config-if)#nos hu

                    ^

% Invalid input detected at '^' marker.

Router(config-if)#no shu                         

Router(config-if)#int s

00:14:17: %LINK-3-UPDOWN: Interface Serial1/3, changed state to up

00:14:18: %LINEPROTO-5-UPDOWN: Line protocol on Interface Serial1/3, changed state to up1/0

Router(config-if)#ip add 192.168.7.1 255.255.255.0

Router(config-if)#no shu

Router(config-if)#

00:14:47: %LINK-3-UPDOWN: Interface Serial1/0, changed state to upint

00:14:48: %LINEPROTO-5-UPDOWN: Line protocol on Interface Serial1/0, changed state to ups1/3

Router(config-if)#ip add 192.168.6.2 255.255.255.0

Router(config-if)#no shu

Router(config-if)#

00:15:11: %LINEPROTO-5-UPDOWN: Line protocol on Interface Serial1/0, changed state to down

Router(config-if)#

Router(config-if)#

Router(config-if)#rou

Router(config-if)#rout

Router(config-if)#ex 

Router(config)#rou

Router(config)#router rip

Router(config-router)#net

Router(config-router)#network 192.168.6.0

Router(config-router)#network 192.168.7.0

Router(config-router)#

R6

Router&gt;

Router&gt;

Router&gt;en

Router#conf t

Enter configuration commands, one per line.  End with CNTL/Z.

Router(config)#hso

Router(config)#hos

Router(config)#hostname R6

R6(config)#int s1/0

R6(config-if)#ip add 192.168.7.2 255.255.255.0

R6(config-if)#no shu

R6(config-if)#no shu

00:16:09: %LINK-3-UPDOWN: Interface Serial1/0, changed state to  

R6(config-if)#no s

00:16:10: %LINEPROTO-5-UPDOWN: Line protocol on Interface Serial1/0, changed state to upint

R6(config-if)#no sint

R6(config-if)#int fa 0/0

R6(config-if)#ip add 192.168.8.1 255.255.255.0

R6(config-if)#no shu

R6#show ip rou

R6#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, 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.8.0/24 is directly connected, FastEthernet0/0

C    192.168.7.0/24 is directly connected, Serial1/0

figuration commands, one per line.  End with CNTL/Z.

R6(config)#rou

R6(config)#router rip

R6(config-router)#net

R6(config-router)#network 192.168.7.0

R6(config-router)#network 192.168.8.0

R6#show ip rou

R6#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, 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.7.1 to network 0.0.0.0

C    192.168.8.0/24 is directly connected, FastEthernet0/0

R    192.168.6.0/24 [120/1] via 192.168.7.1, 00:00:21, Serial1/0

C    192.168.7.0/24 is directly connected, Serial1/0

R*   0.0.0.0/0 [120/2] via 192.168.7.1, 00:00:21, Serial1/0

R6#