路由
路由器的作用
①能够将数据包转发到正确的目的地,并在转发过程中选择最佳路径的设备
②路由器通过查询路由表,实现数据包转发
③隔离广播域
路由表
路由器是根据路由表做路径选择的,是路由器中维护的路由条目的集合。
路由表的形成
直连网段
配置ip地址,端口up状态,形成直连路由
非直连网段
可以通过静态添加和动态学习
静态路由:由管理员手工配置的,单向的,优点:便于管理;缺点:缺乏灵活性。
默认路由:是一种特殊的静态路由,当路由器在路由表中找不到目标网络的路由条目时,路由器把请求转发到默认路由端口
静态路由的相关配置命令
ip route-static 目标网段 子网掩码 下一跳接口地址 静态路由的配置
ip route-static 0.0.0.0 0 下一跳 默认静态路由配置
dis ip route-table 查看路由表
dis arp 查看arp表
浮动静态
静态路由优先级是60,配置浮动静态就是改变优先级参数,使之主线路断开之后,能使网络照样工作。
ip route-static 目标网段 子网掩码 下一跳接口地址 pre 70(优先级) 浮动路由
路由的转发和封装
主机a想要与主机b通信,先查看自己的arp表,没有主机b的mac就发送arp请求广播,路由器a收到后会回复一个arp单播应答,路由器a会检查ip地址,将数据包解封装,源ip地址和目的ip地址不变,检查自己的路由表是否能达到目标ip网段,没有则丢弃,是则将数据包转到准备发出的接口,重新封装后源mac地址变为发出数据接口的mac地址,目的mac未知。所以路由器a继续广播寻找主机b的mac地址,同理路由器 b广播寻找主机b的mac地址,主机b收到会单播回应。回去的包同理,只不过源ip和目的ip对调。
相关实验
1.
首先R1上接口地址
[R1]dis cu
[V200R003C00]
#
sysname R1
#
snmp-agent local-engineid 800007DB03000000000000
snmp-agent
#
clock timezone China-Standard-Time minus 08:00:00
#
portal local-server load portalpage.zip
#
drop illegal-mac alarm
#
set cpu-usage threshold 80 restore 75
#
aaa
authentication-scheme default
authorization-scheme default
accounting-scheme default
domain default
domain default_admin
local-user admin password cipher %$%$K8m.Nt84DZ}e#<0`8bmE3Uw}%$%$
local-user admin service-type http
#
firewall zone Local
priority 15
#
interface GigabitEthernet0/0/0
ip address 192.168.1.1 255.255.255.0
#
interface GigabitEthernet0/0/1
ip address 192.168.2.1 255.255.255.0
#
interface GigabitEthernet0/0/2
ip address 192.168.10.1 255.255.255.0
#
interface NULL0
#
user-interface con 0
authentication-mode password
user-interface vty 0 4
user-interface vty 16 20
#
wlan ac
#
return
[R1]
R2接口地址
[R2]dis cu
[V200R003C00]
#
sysname R2
#
snmp-agent local-engineid 800007DB03000000000000
snmp-agent
#
clock timezone China-Standard-Time minus 08:00:00
#
portal local-server load portalpage.zip
#
drop illegal-mac alarm
#
set cpu-usage threshold 80 restore 75
#
aaa
authentication-scheme default
authorization-scheme default
accounting-scheme default
domain default
domain default_admin
local-user admin password cipher %$%$K8m.Nt84DZ}e#<0`8bmE3Uw}%$%$
local-user admin service-type http
#
firewall zone Local
priority 15
#
interface GigabitEthernet0/0/0
ip address 192.168.10.2 255.255.255.0
#
interface GigabitEthernet0/0/1
ip address 192.168.20.1 255.255.255.0
#
interface GigabitEthernet0/0/2
#
interface NULL0
#
user-interface con 0
authentication-mode password
user-interface vty 0 4
user-interface vty 16 20
#
wlan ac
#
return
[R2]
R3接口地址
[R3]dis cu
[V200R003C00]
#
sysname R3
#
snmp-agent local-engineid 800007DB03000000000000
snmp-agent
#
clock timezone China-Standard-Time minus 08:00:00
#
portal local-server load portalpage.zip
#
drop illegal-mac alarm
#
set cpu-usage threshold 80 restore 75
#
aaa
authentication-scheme default
authorization-scheme default
accounting-scheme default
domain default
domain default_admin
local-user admin password cipher %$%$K8m.Nt84DZ}e#<0`8bmE3Uw}%$%$
local-user admin service-type http
#
firewall zone Local
priority 15
#
interface GigabitEthernet0/0/0
ip address 192.168.20.2 255.255.255.0
#
interface GigabitEthernet0/0/1
ip address 10.0.0.1 255.0.0.0
#
interface GigabitEthernet0/0/2
#
interface NULL0
#
user-interface con 0
authentication-mode password
user-interface vty 0 4
user-interface vty 16 20
#
wlan ac
#
return
[R3]
此时三个路由器中的路由表都只有直连路由
[R1]dis ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: Public
Destinations : 13 Routes : 13
Destination/Mask Proto Pre Cost Flags NextHop Interface
127.0.0.0/8 Direct 0 0 D 127.0.0.1 InLoopBack0
127.0.0.1/32 Direct 0 0 D 127.0.0.1 InLoopBack0
127.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
192.168.1.0/24 Direct 0 0 D 192.168.1.1 GigabitEthernet
0/0/0
192.168.1.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
192.168.1.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
192.168.2.0/24 Direct 0 0 D 192.168.2.1 GigabitEthernet
0/0/1
192.168.2.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
192.168.2.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
192.168.10.0/24 Direct 0 0 D 192.168.10.1 GigabitEthernet
0/0/2
192.168.10.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/2
192.168.10.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/2
255.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
[R1]
[R2]dis ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: Public
Destinations : 10 Routes : 10
Destination/Mask Proto Pre Cost Flags NextHop Interface
127.0.0.0/8 Direct 0 0 D 127.0.0.1 InLoopBack0
127.0.0.1/32 Direct 0 0 D 127.0.0.1 InLoopBack0
127.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
192.168.10.0/24 Direct 0 0 D 192.168.10.2 GigabitEthernet
0/0/0
192.168.10.2/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
192.168.10.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
192.168.20.0/24 Direct 0 0 D 192.168.20.1 GigabitEthernet
0/0/1
192.168.20.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
192.168.20.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
255.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
[R2]
[R3]dis ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: Public
Destinations : 10 Routes : 10
Destination/Mask Proto Pre Cost Flags NextHop Interface
10.0.0.0/8 Direct 0 0 D 10.0.0.1 GigabitEthernet
0/0/1
10.0.0.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
10.255.255.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
127.0.0.0/8 Direct 0 0 D 127.0.0.1 InLoopBack0
127.0.0.1/32 Direct 0 0 D 127.0.0.1 InLoopBack0
127.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
192.168.20.0/24 Direct 0 0 D 192.168.20.2 GigabitEthernet
0/0/0
192.168.20.2/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
192.168.20.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
255.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
[R3]
在R1上配置默认路由
[R1]ip route-static 0.0.0.0 0 192.168.10.2
查看路由表,可以看到默认路由写上去了
[R1]dis ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: Public
Destinations : 14 Routes : 14
Destination/Mask Proto Pre Cost Flags NextHop Interface
0.0.0.0/0 Static 60 0 RD 192.168.10.2 GigabitEthernet
0/0/2
127.0.0.0/8 Direct 0 0 D 127.0.0.1 InLoopBack0
127.0.0.1/32 Direct 0 0 D 127.0.0.1 InLoopBack0
127.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
192.168.1.0/24 Direct 0 0 D 192.168.1.1 GigabitEthernet
0/0/0
192.168.1.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
192.168.1.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
192.168.2.0/24 Direct 0 0 D 192.168.2.1 GigabitEthernet
0/0/1
192.168.2.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
192.168.2.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
192.168.10.0/24 Direct 0 0 D 192.168.10.1 GigabitEthernet
0/0/2
192.168.10.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/2
192.168.10.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/2
255.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
[R1]
R2的配置及路由表,可以看到有到1.0和2.0和sever的静态
[R2]ip route-static 192.168.1.0 24 192.168.10.1
[R2]ip route-static 192.168.2.0 24 192.168.10.1
[R2]ip route-static 10.0.0.0 8 192.168.20.2
[R2]dis ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: Public
Destinations : 13 Routes : 13
Destination/Mask Proto Pre Cost Flags NextHop Interface
10.0.0.0/8 Static 60 0 RD 192.168.20.2 GigabitEthernet
0/0/1
127.0.0.0/8 Direct 0 0 D 127.0.0.1 InLoopBack0
127.0.0.1/32 Direct 0 0 D 127.0.0.1 InLoopBack0
127.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
192.168.1.0/24 Static 60 0 RD 192.168.10.1 GigabitEthernet
0/0/0
192.168.2.0/24 Static 60 0 RD 192.168.10.1 GigabitEthernet
0/0/0
192.168.10.0/24 Direct 0 0 D 192.168.10.2 GigabitEthernet
0/0/0
192.168.10.2/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
192.168.10.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
192.168.20.0/24 Direct 0 0 D 192.168.20.1 GigabitEthernet
0/0/1
192.168.20.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
192.168.20.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
255.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
R3的配置及路由表,可以看到到1.0和2.0和10.0的静态路由
[R3]ip route-static 192.168.10.0 24 192.168.20.1
[R3]ip route-static 192.168.1.0 24 192.168.20.1
[R3]ip route-static 192.168.2.0 24 192.168.20.1
[R3]dis ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: Public
Destinations : 13 Routes : 13
Destination/Mask Proto Pre Cost Flags NextHop Interface
10.0.0.0/8 Direct 0 0 D 10.0.0.1 GigabitEthernet
0/0/1
10.0.0.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
10.255.255.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
127.0.0.0/8 Direct 0 0 D 127.0.0.1 InLoopBack0
127.0.0.1/32 Direct 0 0 D 127.0.0.1 InLoopBack0
127.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
192.168.1.0/24 Static 60 0 RD 192.168.20.1 GigabitEthernet
0/0/0
192.168.2.0/24 Static 60 0 RD 192.168.20.1 GigabitEthernet
0/0/0
192.168.10.0/24 Static 60 0 RD 192.168.20.1 GigabitEthernet
0/0/0
192.168.20.0/24 Direct 0 0 D 192.168.20.2 GigabitEthernet
0/0/0
192.168.20.2/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
192.168.20.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
255.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
[R3]
此时pc1 pc2可以与sever通信