查看: 3887|回复: 3

同一个网络中不同网段主机通信

[复制链接]
发表于 2008-8-9 23:00:04 | 显示全部楼层 |阅读模式
同一个网络中不同网段主机通信一般我们都说在同一个网络只不同网段的主机不能通信,那我们今天来了解一下为什么不能通信,又使用什么方法可以让它通信。
首先来说明一下为什么不能通信,主机其实和路由器是一样,但是一般情况下不会转发数据包,安装特定的软件也可实现转发的功能,他内部也有一个路由表(可以在cmd下面使用route print来查看)配置网关相当于增加了一条默认路由,这条默认路由的下一跳就是网关的IP地址,当发通数据的时候象路由器一样查找路由表,如果没有精确路由的话就会走默认路由,这个时候数据包就会发送给网关,网关上和主机做的过程一样,在路由表中没有找到到达目标网段的路由,如果配置了默认路由就使用这条默认路由发送。这样数据包就会被发送出外网去了。所以这个不同网段的主机和自己在一个网络里面的时候就不能通信。

看了上面不能通信的说明之后我们找到了关键点,也就是没有到达目标网段的路由,使用默认路由发送到了外网。笔记找到了四个解决的方法。
1、
把对方设置为网关
001.JPG (22.44 KB)
2008-8-9 22:45




说明:这是配置网关后增加的默认路由,如下图
007.JPG (11.34 KB)
2008-8-9 22:45




PC1发数一个数据包给PC2的时候,PC1在自己的路由表中没有找到到达PC2这个网段的路由,就会走默认路由把数据包发送给网关PC2(这里PC认为网关是和他在一个网络中的,所以这里不需要查找路由直接把数据包送出去),发送一个数据包需要在ARP缓存里面找到目标IP地址和目标MAC的映射,如果没有这个映射就会发送ARP请求查找。所以这个时候PC1就会发出一个ARP请求包去查找PC2MAC地址。


002.JPG (31.91 KB)
2008-8-9 22:45


PC2收到这个ARP请求包之后就会回复一个ARP响应。


003.JPG (32.43 KB)
2008-8-9 22:45


PC1有了这个MAC地址之后封装数据帧发送给PC2,当PC2收到这个数据包之后。查看三层头的目的地址,一看是自己的就提交给上层处理。PC2发数据包给PC1也是一样的过程。


2、
把自己设置为网关


004.JPG (23.47 KB)
2008-8-9 22:45


说明:当把自己设为网关的时候就象在路由器上加路由时指定的是出接口。他只知道没有路由的网段就从自己网关对应的接口发送出去。但是可能从这个接口出去有很多个出口可以到对方。这个时候的它就会发送一个ARP请求。

002.JPG (31.91 KB)
2008-8-9 22:51



其实这时他是为了找代理ARP。但是当发送这个ARP请求时对方也会收到。既然目标是自己那肯定要回复了。
003.JPG (32.43 KB)
2008-8-9 22:51




那这时PC1就会收到这个ARP回复,它认为这是一个出口那么他就会把数据封装了之后发给PC2,而在PC2收到这个数据包后一看目的IP地址为自己,就送到上层处理。
3、
设置出口路由器为网关,在路由器上加路由


005.JPG (25 KB)
2008-8-9 22:45


在这里我们使用三个路由器来做。
R1的配置
R1#sho run
Building configuration...

Current configuration : 576 bytes
!
version 12.4
service timestamps debug datetime msec
service timestamps log datetime msec
no service password-encryption
!
hostname R1
!
boot-start-marker
boot-end-marker
!
!
no aaa new-model
memory-size iomem 5
!
!
ip cef
no ip domain lookup
!
interface FastEthernet0/0

ip address 192.168.3.1 255.255.255.0


duplex auto


speed auto

!
ip http server
ip route 192.168.1.0 255.255.255.0 FastEthernet0/0
ip route 192.168.2.0 255.255.255.0 FastEthernet0/0
!
control-plane
!
line con 0
line aux 0
line vty 0 4
!
End



PC1配置
Building configuration...

Current configuration : 541 bytes
!
version 12.4
service timestamps debug datetime msec
service timestamps log datetime msec
no service password-encryption
!
hostname R2
!
boot-start-marker
boot-end-marker
!
!
no aaa new-model
memory-size iomem 5
no ip routing
!
!
no ip cef
no ip domain lookup
!
interface FastEthernet0/0

ip address 192.168.1.1 255.255.255.0


no ip route-cache


duplex auto


speed auto

!
ip default-gateway 192.168.3.1
ip http server
!
control-plane
!
line con 0
line aux 0
line vty 0 4
!
!
End



PC2配置
R3#sho run
Building configuration...

Current configuration : 541 bytes
!
version 12.4
service timestamps debug datetime msec
service timestamps log datetime msec
no service password-encryption
!
hostname R3
!
boot-start-marker
boot-end-marker
!
!
no aaa new-model
memory-size iomem 5
no ip routing
!
!
no ip cef
no ip domain lookup
!
interface FastEthernet0/0

ip address 192.168.2.1 255.255.255.0


no ip route-cache


duplex auto


speed auto

!
ip default-gateway 192.168.3.1
ip http server
!
control-plane
!
line con 0
line aux 0
line vty 0 4
!
!
End

这里的过程和上面差不多,PC1PC2把数据给网关,通过网关转发。
R2#ping 192.168.2.1

Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.2.1, timeout is 2 seconds:

//
第一个超时,这是因为没有网关的MAC地址
!
*Mar
1 00:11:30.031: IP ARP: creating incomplete entry for IP address: 192.168.3.1 interface FastEthernet0/0

*Mar
1 00:11:30.031: IP ARP: sent req src 192.168.1.1 cc00.0ff8.0000,


dst 192.168.3.1 0000.0000.0000 FastEthernet0/0

//
发送一个ARP发请求网关地址
*Mar
1 00:11:30.191: IP ARP: rcvd rep src 192.168.3.1 cc00.0a64.0000, dst 192.168.1.1 FastEthernet0/0!!!!
//
收到网关回复的ARP,后面的四个包通了。
Success rate is 100 percent (5/5), round-trip min/avg/max = 60/288/1072 ms
R2#sho arp
Protocol
Address
Age (min)
Hardware Addr
Type
Interface

Internet
192.168.1.1

-
cc00.0ff8.0000
ARPA
FastEthernet0/0

Internet
192.168.3.1
0
cc00.0a64.0000
ARPA
FastEthernet0/0


过程和上面两个差不多,在这里就不说了。
注意两点:
一、可以把上面的两条路由改成一条
ip route 0.0.0.0 0.0.0.0 FastEthernet0/0
二、要说的是当在网关上配置路由器配置的是下一跳地址。
ip route 192.168.1.0 255.255.255.0 192.168.1.1
ip route 192.168.2.0 255.255.255.0 192.168.2.1
是肯定通不了的,因为这个路由器上没到192.168.1.1路由,所以查看路由表的时候只有192.168.3.0/24这条直连路由。


4、
配置从地址


006.JPG (24.31 KB)
2008-8-9 22:45


R1配置
Building configuration...

Current configuration : 574 bytes
!
version 12.4
service timestamps debug datetime msec
service timestamps log datetime msec
no service password-encryption
!
hostname R1
!
boot-start-marker
boot-end-marker
!
!
no aaa new-model
memory-size iomem 5
!
!
ip cef
no ip domain lookup
!
interface FastEthernet0/0

ip address 192.168.1.254 255.255.255.0 secondary


ip address 192.168.2.254 255.255.255.0 secondary


ip address 192.168.3.1 255.255.255.0


duplex auto


speed auto

!
ip http server
!

line con 0
line aux 0
line vty 0 4
!
!
End

PC1配置
Building configuration...

Current configuration : 543 bytes
!
version 12.4
service timestamps debug datetime msec
service timestamps log datetime msec
no service password-encryption
!
hostname R2
!
boot-start-marker
boot-end-marker
!
!
no aaa new-model
memory-size iomem 5
no ip routing
!
!
no ip cef
no ip domain lookup
!
interface FastEthernet0/0

ip address 192.168.1.1 255.255.255.0


no ip route-cache


duplex auto


speed auto

!
ip default-gateway 192.168.1.254
ip http server
!
control-plane
!
line con 0
line aux 0
line vty 0 4
!
!
End


PC2配置
Building configuration...

Current configuration : 543 bytes
!
version 12.4
service timestamps debug datetime msec
service timestamps log datetime msec
no service password-encryption
!
hostname R3
!
boot-start-marker
boot-end-marker
!
!
no aaa new-model
memory-size iomem 5
no ip routing
!
!
no ip cef
no ip domain lookup
!
interface FastEthernet0/0

ip address 192.168.2.1 255.255.255.0


no ip route-cache


duplex auto


speed auto

!
ip default-gateway 192.168.2.254
ip http server
!
control-plane
!
line con 0
line aux 0
line vty 0 4
!
!
End

这里的过程和上面差不多,PC1PC2把数据给网关,通过网关转发。
如果你有其它方法。不妨也共享一下大家学习学习。

[ 本帖最后由 huyangyang 于 2008-8-9 23:01 编辑 ]
回复

使用道具 举报

发表于 2008-8-11 09:52:13 | 显示全部楼层
感觉阁下是资深人士
回复

使用道具 举报

发表于 2008-8-11 11:13:58 | 显示全部楼层

感觉阁下是资深人士

回复

使用道具 举报

发表于 2008-8-16 01:13:33 | 显示全部楼层
dddddddddddd
回复

使用道具 举报

您需要登录后才可以回帖 登录 | CSNA会员注册

本版积分规则

快速回复 返回顶部 返回列表