配置OSPF路由

OSPF路由

1.OSPF路由

1.1 OSPF简介

OSPF(Open Shortest Path First,开放式最短路径优先)路由协议是另一个比较常用的路由协议之一,它通过路由器之间通告网络接口的状态,使用最短路径算法建立路由表。在生成路由表时,OSPF协议优先考虑线路的速率等因素(费用),而经过的跳数则不是重点参考条件。

OSPF 路由协议可以支持在一个自治区域中运行,也可以支持在多个自治区域之间运行。

下面主要介绍单区域内OSPF的配置方法。

如图1所示,在网络拓扑图中,每个路由器都使用OSPF 协议生成路由表,其中RouterO与Router3之间线路的速率比较慢(费用比较高,为100),而其他三条线路的速率比较快(费用比较小,每条都是10)。

2.PC设备配置

2.1 PC设备信息及接口配置

配置PC设备的接口信息,如表1-1所示。

|--------|------------|---------------|---------------|-------------|
| 设备 | 连接的路由器 | IP 地址 | 子网掩码 | 网关地址 |
| PC1 | R1 | 192.168.1.2 | 255.255.255.0 | 192.168.1.1 |
| PC2 | R1 | 192.168.2.2 | 255.255.255.0 | 192.168.2.1 |
| PC3 | R2 | 172.16.0.2 | 255.255.0.0 | 172.16.0.1 |
| PC4 | R3 | 172.18.0.2 | 255.255.0.0 | 172.18.0.1 |
| PC5 | R4 | 172.17.0.2 | 255.255.0.0 | 172.17.0.1 |

表1-1

3.路由器接口配置

3.1 路由器接口信息及配置

配置路由器设备的接口信息,如表1-2所示。

|---------|----------------------|---------------|---------------|-----------------|----------|
| 路由器 | 接口 | IP 地址 | 子网掩码 | OSPF 费用 | 连接设备 |
| R1 | GigabitEthernet0/0/0 | 192.168.1.1 | 255.255.255.0 | - | 连接到设备PC1 |
| R1 | GigabitEthernet0/0/1 | 192.168.2.1 | 255.255.255.0 | - | 连接到设备PC2 |
| R1 | Serial0/1/0 | 13.0.0.2 | 255.0.0.0 | 100 | 连接到设备R4 |
| R1 | Serial0/1/1 | 10.0.0.1 | 255.0.0.0 | 10 | 连接到设备R2 |
| R2 | GigabitEthernet0/0/0 | 172.16.0.1 | 255.255.0.0 | - | 连接到设备PC3 |
| R2 | Serial0/1/0 | 11.0.0.1 | 255.0.0.0 | 10 | 连接到设备R3 |
| R2 | Serial0/1/1 | 10.0.0.2 | 255.0.0.0 | 10 | 连接到设备R1 |
| R3 | GigabitEthernet0/0/0 | 172.18.0.1 | 255.255.0.0 | - | 连接到设备PC4 |
| R3 | Serial0/1/1 | 11.0.0.2 | 255.0.0.0 | 10 | 连接到设备R2 |
| R3 | Serial0/1/0 | 12.0.0.1 | 255.0.0.0 | 10 | 连接到设备R4 |
| R4 | GigabitEthernet0/0/0 | 172.17.0.1 | 255.255.0.0 | - | 连接到设备PC5 |
| R4 | Serial0/1/1 | 13.0.0.1 | 255.0.0.0 | - | 连接到设备R1 |
| R4 | Serial0/1/0 | 12.0.0.2 | 255.0.0.0 | - | 连接到设备R3 |

表1-2

图1 使用OSPF生成路由表的网络拓扑图

4.OSPF配置

4.1 OSPF费用配置

然后在指定接口的配置模式下,使用"ip ospf cost 费用"命令为每一个接口上的线路配置费用。

在Router0 中配置接口的我用,其中 Se1/0接口连接的线路费用是100,Se0/0费用是10.

R1(config)#interface s0/0

R1(config-if) #ip ospf cost 10

R1(config-if)#exit

R1(config) #interface s1/0

R1(config-if)#ip ospf cost 100

在R2中配置全部接口的费用都是10。

R2(config)#interface s0/0

R2(config-if)#ip ospf cost 10

R2(config-if)#exit

Routerl(config)#interface s1/0

Routerl(config-if)#ip ospf cost 10

在R3中配置全部接口的费用都是10。

R3(config) #interface s0/0

R3(config-if)#ip ospf cost 10

R3(config-if)#exit

R3(config)#interface s1/0

R3(config-if)#ip ospf cost 10

4.2 OSPF网络配置

最后在每个路由器中使用router ospf命令,其后面需要指定一个数字作为OSPF进程的进程号,这样就可以进入指定进程号的OSPF配置环境中了。在这个配置环境中,同样使用network广播本地路由器直接连接的网络IP地址,其后的参数不是子网掩码,而是使用"area区域号"作为最后一个参数(由于实例是在一个区域中,即单区域,因此其区域号都设置为1)。

在路由器R1中配置OSPF协议。

R1(config)# router ospf 1

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)#network 10.0.0.0 0.255.255.255 area 1

R1(config-router)#network 11.0.0.0 0.255.255.255 area 1

在路由器 Routerl中配置 OSPF协议。

R2(config) #router ospf 1

Routerl(config-router)#network 172.16.0.0 0.0.255.255 area 1

Roüterl(config-router)#network 10.0.0.0 0.255.255.255 area 1

Routerl(config-router)#network 11.0.0.0 0.255.255.255 area 1

在路由器R3中配置OSPF协议。

outer(config)#router ospf 1

R3(config-router)#network 172.17.0.0 0.0.255.255 area 1

R3(config-router)#network 11.0.0.00.255.255.255 area 1

R3(config-router)#network 12.0.0.0 0.255.255.255 area 1

5.路由表查看

5.1 查看路由表

查看路由器R1中的路由表,其中以O开头的路由记录都是由OSPF协议计算得

到的。

R1#show ip route ospf

O 11.0.0.0 110/20 via 10.0.0.2, 00:30:17, Serial0/1/1

O 12.0.0.0 110/30 via 10.0.0.2, 00:30:07, Serial0/1/1

O 172.16.0.0 110/11 via 10.0.0.2, 00:30:17, Serial0/1/1

O 172.17.0.0 110/31 via 10.0.0.2, 00:30:07, Serial0/1/1

O 172.18.0.0 110/21 via 10.0.0.2, 00:30:07, Serial0/1/1

R1#

结合线路的费用,OSPF 协议没有使用R1与R4之间的线路,而是使用了R1→R2→R3→R4路径(到 172.17.0.0 网络的下一跳路由是R2,其入口地址是10.0.0.2)。

在 PC1中使用tracert 命令检查实际路径是否与路由表中的记录相符合。

Cisco Packet Tracer PC Command Line 1.0

C:\>tracert 172.18.0.2

Tracing route to 172.18.0.2 over a maximum of 30 hops:

1 0 ms 0 ms 0 ms 192.168.2.1

2 0 ms 0 ms 5 ms 10.0.0.2

3 0 ms 20 ms 1 ms 11.0.0.2

4 * 11 ms 0 ms 172.18.0.2

Trace complete.

C:\>

路由器中还提供了多条命令用于查看OSPF协议的详细信息。

Show ip ospf neighbor 可以显示本地路由的OSPF邻居的信息,包括它们的路由器ID,接口地址和IP地址等。

R1#show ip ospf neighbor

Neighbor ID Pri State Dead Time Address Interface

2.2.2.2 0 FULL/ - 00:00:30 10.0.0.2 Serial0/1/1

4.4.4.4 0 FULL/ - 00:00:31 13.0.0.1 Serial0/1/0

R1#

Show ip ospf database 用于显示本地路由的OSPF库内容(与路由表内容相似)。

R1#show ip ospf database

OSPF Router with ID (1.1.1.1) (Process ID 1)

Router Link States (Area 1)

Link ID ADV Router Age Seq# Checksum Link count

1.1.1.1 1.1.1.1 607 0x80000008 0x00ac4f 6

4.4.4.4 4.4.4.4 608 0x80000007 0x009f4e 5

3.3.3.3 3.3.3.3 607 0x80000007 0x00cbfd 5

2.2.2.2 2.2.2.2 607 0x80000007 0x006679 5

R1#

Show ip protocols 命令用于显示与路由协议相关的参数与定时器信息,本命令也可以在启用了RIP路由协议的路由器中使用。

R1#show ip protocols

Routing Protocol is "ospf 1"

Outgoing update filter list for all interfaces is not set

Incoming update filter list for all interfaces is not set

Router ID 1.1.1.1

Number of areas in this router is 1. 1 normal 0 stub 0 nssa

Maximum path: 4

Routing for Networks:

192.168.1.0 0.0.0.255 area 1

192.168.2.0 0.0.0.255 area 1

10.0.0.0 0.0.0.255 area 1

13.0.0.0 0.0.0.255 area 1

Routing Information Sources:

Gateway Distance Last Update

1.1.1.1 110 00:15:02

2.2.2.2 110 00:15:01

3.3.3.3 110 00:15:03

4.4.4.4 110 00:15:03

Distance: (default is 110)

R1#

6.实验脚本

6.1 脚本示例

//R1

复制代码
en

conf t

hostn R1

int g0/0/0

ip add 192.168.1.1 255.255.255.0

no sh

int g0/0/1

ip add 192.168.2.1 255.255.255.0

no sh

int s0/1/0

ip add 13.0.0.2 255.0.0.0

no sh

ip ospf cost 100

int s0/1/1

ip add 10.0.0.1 255.0.0.0

no sh

ip ospf cost 10

exit

router ospf 1

router-id 1.1.1.1

network 192.168.1.0 0.0.0.255 area 1

network 192.168.2.0 0.0.0.255 area 1

network 10.0.0.0 255.255.255.0 area 1

network 13.0.0.0 255.255.255.0 area 1

end

w

!

//R2

复制代码
en

conf t

hostn R2

int g0/0/0

ip add 172.16.0.1 255.255.0.0

no sh

int s0/1/0

ip add 11.0.0.1 255.0.0.0

no sh

ip ospf cost 10

int s0/1/1

ip add 10.0.0.2 255.0.0.0

no sh

ip ospf cost 10

exit

router ospf 1

router-id 2.2.2.2

network 172.16.0.0 0.0.255.255 area 1

network 11.0.0.0 255.255.255.0 area 1

network 10.0.0.0 255.255.255.0 area 1

end

w

!

//R3

复制代码
en

conf t

hostn R3

int g0/0/0

ip add 172.18.0.1 255.255.0.0

no sh

int s0/1/1

ip add 11.0.0.2 255.0.0.0

no sh

ip ospf cost 10

int s0/1/0

ip add 12.0.0.1 255.0.0.0

no sh

ip ospf cost 10

exit

router ospf 1

router-id 3.3.3.3

network 172.18.0.0 0.0.255.255 area 1

network 11.0.0.0 255.255.255.0 area 1

network 12.0.0.0 255.255.255.0 area 1

end

w

!

//R4

复制代码
en

conf t

hostn R4

int g0/0/0

ip add 172.17.0.1 255.255.0.0

no sh

int s0/1/1

ip add 13.0.0.1 255.0.0.0

no sh

int s0/1/0

ip add 12.0.0.2 255.0.0.0

no sh

exit

router ospf 1

router-id 4.4.4.4

network 172.17.0.0 0.0.255.255 area 1

network 12.0.0.0 255.255.255.0 area 1

network 13.0.0.0 255.255.255.0 area 1

end

w

!

当面对挑战时,不要害怕失败,因为每次失败都是成功的一步。相信自己,勇往直前,你能够战胜一切!

相关推荐
huluang4 小时前
密评多选题 — 陷阱名单(费曼自述法版)
网络·数据库·密码学
yyuuuzz5 小时前
AI模型部署中的常见稳定性问题
运维·服务器·网络·数据库·人工智能·云计算·github
ylscode5 小时前
HexStrike AI v6.0 深度解析:MCP协议驱动的网络安全自动化框架与红队规避实战
网络·人工智能·安全·安全威胁分析
8125035335 小时前
第 8 篇:IP 地址:互联网的门牌号
网络·网络协议·tcp/ip
liulilittle5 小时前
什么是“单流”?一个服务器上能不能同时存在多个“单流”?
服务器·网络·tcp/ip·计算机网络·信息与通信·tcp·通信
梁辰兴6 小时前
计算机网络基础:基于万维网的电子邮件
网络·计算机网络·计算机网络基础·梁辰兴·基于万维网的电子邮件·webmail
KaMeidebaby6 小时前
卡梅德生物技术快报|细胞周期检测抗原流式分析:参数调试、软件拟合与问题排查
网络·人工智能·python·网络协议·tcp/ip·算法·机器学习
梁辰兴6 小时前
计算机网络基础:邮件读取协议 POP3和IMAP
网络·计算机网络·imap·pop3·计算机网络基础·梁辰兴·邮件读取协议
艾莉丝努力练剑6 小时前
【QT】界面优化:QSS
linux·运维·开发语言·网络·qt·计算机网络·udp
Irissgwe6 小时前
7、传输层协议 TCP
网络·网络协议·tcp/ip·tcp·三次握手·四次挥手