4)所有的配置已经完成,我们在①号机ping④号机
ping10.0.4.3
结果如下,即ping不通:
PING10.0.4.3(10.0.4.3)56(84)bytesofdata.From10.0.1.3icmp_seq=2DestinationHostUnreachableFrom10.0.1.3icmp_seq=3DestinationHostUnreachableFrom10.0.1.3icmp_seq=4DestinationHostUnreachableFrom10.0.1.3icmp_seq=6DestinationHostUnreachableFrom10.0.1.3icmp_seq=7DestinationHostUnreachableFrom10.0.1.3icmp_seq=8DestinationHostUnreachable
这里为了方便研究,把①号机的eth1配置放出来
eth1Linkencap:EthernetHWaddr00:16:EC:AF:CB:CBinetaddr:10.0.1.3Bcast:10.255.255.255Mask:255.255.255.0inet6addr:fe80::216:ecff:feaf:cbcb/64Scope:LinkUPBROADCASTRUNNINGMULTICASTMTU:1500Metric:1RXpackets:4564errors:0dropped:0overruns:0frame:0TXpackets:6688errors:0dropped:0overruns:0carrier:0collisions:0txqueuelen:1000RXbytes:459463(448.6KiB)TXbytes:546633(533.8KiB)Interrupt:23Baseaddress:0x6000
在①号机ping的同时,我在④号机抓eth1包,结果如下:
[root@h4~]#tcpdump-ieth1-enntcpdump:verboseoutputsuppressed,use-vor-vvforfullprotocoldecodelisteningoneth1,link-typeEN10MB(Ethernet),capturesize65535bytes15:26:44.38861400:16:ec:af:cb:cb》ff:ff:ff:ff:ff:ff,ethertypeARP(0x0806),length60:Requestwho-has10.0.4.3tell10.0.1.3,length4615:26:45.39101400:16:ec:af:cb:cb》ff:ff:ff:ff:ff:ff,ethertypeARP(0x0806),length60:Requestwho-has10.0.4.3tell10.0.1.3,length4615:26:47.38782100:16:ec:af:cb:cb》ff:ff:ff:ff:ff:ff,ethertypeARP(0x0806),length60:Requestwho-has10.0.4.3tell10.0.1.3,length4615:26:48.39122000:16:ec:af:cb:cb》ff:ff:ff:ff:ff:ff,ethertypeARP(0x0806),length60:Requestwho-has10.0.4.3tell10.0.1.3,length4615:26:49.39262100:16:ec:af:cb:cb》ff:ff:ff:ff:ff:ff,ethertypeARP(0x0806),length60:Requestwho-has10.0.4.3tell10.0.1.3,length46
可见①号一直在寻找配有10.0.4.3IP的机子的mac地址,即一直在发arp包。但是路由器(④号机)默认是不转发arp报文的,所有①号机永远也ping不通⑦号机。
3、正确的配置
在①号机种配置路由,命令如下:
routeadd-net10.0.4.0/24gw10.0.1.2
这时候①号机的路由表:
[root@h1~]#[root@h1~]#route-nKernelIProutingtableDestinationGatewayGenmaskFlagsMetricRefUseIface10.0.4.010.0.1.2255.255.255.0UG000eth110.0.5.00.0.0.0255.255.255.0U000eth210.0.1.00.0.0.0255.255.255.0U000eth1192.168.99.00.0.0.0255.255.255.0U100eth00.0.0.0192.168.99.10.0.0.0UG000eth0
同样的方法配置⑦号机的路由表
root@h7:~#route-n内核IP路由表目标网关子网掩码标志跃点引用使用接口0.0.0.0192.168.99.10.0.0.0UG000eth010.0.1.010.0.4.2255.255.255.0UG000eth110.0.4.00.0.0.0255.255.255.0U100eth110.0.7.00.0.0.0255.255.255.0U100eth2192.168.99.00.0.0.0255.255.255.0U100eth0
下面再进行ping测试,在①号机ping⑦号机,结果能够ping通。在这里我们问了方便分析,首先列出各网卡的MAC地址
①号机eth1:HWaddr00:16:EC:AF:CB:CB④号机eth1:HWaddr40:61:86:32:8F:0B④号机eth4:HWaddr40:61:86:32:8F:0E⑦号机eth1:HWaddr00:25:90:93:40:79
④号机eth1抓包如下:
[root@h4~]#tcpdump-ieth1-enntcpdump:verboseoutputsuppressed,use-vor-vvforfullprotocoldecodelisteningoneth1,link-typeEN10MB(Ethernet),capturesize65535bytes16:02:26.80944500:16:ec:af:cb:cb》40:61:86:32:8f:0b,ethertypeIPv4(0x0800),length98:10.0.1.3》10.0.4.3:ICMPechorequest,id8079,seq1,length6416:02:26.81072340:61:86:32:8f:0b》00:16:ec:af:cb:cb,ethertypeIPv4(0x0800),length98:10.0.4.3》10.0.1.3:ICMPechoreply,id8079,seq1,length6416:02:27.81184700:16:ec:af:cb:cb》40:61:86:32:8f:0b,ethertypeIPv4(0x0800),length98:10.0.1.3》10.0.4.3:ICMPechorequest,id8079,seq2,length6416:02:27.81313640:61:86:32:8f:0b》00:16:ec:af:cb:cb,ethertypeIPv4(0x0800),length98:10.0.4.3》10.0.1.3:ICMPechoreply,id8079,seq2,length6416:02:28.81324800:16:ec:af:cb:cb》40:61:86:32:8f:0b,ethertypeIPv4(0x0800),length98:10.0.1.3》10.0.4.3:ICMPechorequest,id8079,seq3,length6416:02:28.81455140:61:86:32:8f:0b》00:16:ec:af:cb:cb,ethertypeIPv4(0x0800),length98:10.0.4.3》10.0.1.3:ICMPechoreply,id8079,seq3,length6416:02:29.81464800:16:ec:af:cb:cb》40:61:86:32:8f:0b,ethertypeIPv4(0x0800),length98:10.0.1.3》10.0.4.3:ICMPechorequest,id8079,seq4,length64
④号机eth4抓包如下:
root@h4~]#tcpdump-ieth4-enntcpdump:verboseoutputsuppressed,use-vor-vvforfullprotocoldecodelisteningoneth4,link-typeEN10MB(Ethernet),capturesize65535bytes16:02:26.80946040:61:86:32:8f:0e》00:25:90:93:40:79,ethertypeIPv4(0x0800),length98:10.0.1.3》10.0.4.3:ICMPechorequest,id8079,seq1,length6416:02:26.81071500:25:90:93:40:79》40:61:86:32:8f:0e,ethertypeIPv4(0x0800),length98:10.0.4.3》10.0.1.3:ICMPechoreply,id8079,seq1,length6416:02:27.81185340:61:86:32:8f:0e》00:25:90:93:40:79,ethertypeIPv4(0x0800),length98:10.0.1.3》10.0.4.3:ICMPechorequest,id8079,seq2,length6416:02:27.81313000:25:90:93:40:79》40:61:86:32:8f:0e,ethertypeIPv4(0x0800),length98:10.0.4.3》10.0.1.3:ICMPechoreply,id8079,seq2,length6416:02:28.81325540:61:86:32:8f:0e》00:25:90:93:40:79,ethertypeIPv4(0x0800),length98:10.0.1.3》10.0.4.3:ICMPechorequest,id8079,seq3,length6416:02:28.81454500:25:90:93:40:79》40:61:86:32:8f:0e,ethertypeIPv4(0x0800),length98:10.0.4.3》10.0.1.3:ICMPechoreply,id8079,seq3,length64
⑦号机eth1抓包如下:
root@h7:~#tcpdump-ieth1-enntcpdump:verboseoutputsuppressed,use-vor-vvforfullprotocoldecodelisteningoneth1,link-typeEN10MB(Ethernet),capturesize65535bytes16:02:27.22285340:61:86:32:8f:0e》00:25:90:93:40:79,ethertypeIPv4(0x0800),length98:10.0.1.3》10.0.4.3:ICMPechorequest,id8079,seq1,length6416:02:27.22286700:25:90:93:40:79》40:61:86:32:8f:0e,ethertypeIPv4(0x0800),length98:10.0.4.3》10.0.1.3:ICMPechoreply,id8079,seq1,length6416:02:28.22522640:61:86:32:8f:0e》00:25:90:93:40:79,ethertypeIPv4(0x0800),length98:10.0.1.3》10.0.4.3:ICMPechorequest,id8079,seq2,length6416:02:28.22523700:25:90:93:40:79》40:61:86:32:8f:0e,ethertypeIPv4(0x0800),length98:10.0.4.3》10.0.1.3:ICMPechoreply,id8079,seq2,length6416:02:29.22663840:61:86:32:8f:0e》00:25:90:93:40:79,ethertypeIPv4(0x0800),length98:10.0.1.3》10.0.4.3:ICMPechorequest,id8079,seq3,length6416:02:29.22664900:25:90:93:40:79》40:61:86:32:8f:0e,ethertypeIPv4(0x0800),length98:10.0.4.3》10.0.1.3:ICMPechoreply,id8079,seq3,length6416:02:30.22805940:61:86:32:8f:0e》00:25:90:93:40:79,ethertypeIPv4(0x0800),length98:10.0.1.3》10.0.4.3:ICMPechorequest,id8079,seq4,length64
从抓取的包中我们不难看出,①号机在ping⑦号机时,由于其中路由表配置了通过四号机的eth1(10.0.1.2)地址,这个地址对应的mac①号机已经缓存了,所有没有进行arp广播就直接开始发送ICMP包,并且目的ip是⑦号机,目的MAC是④号机的eth1的,之后在④号机路由中又将目的MAC变成了④号机的eth4的,目的ip不变,回来的过程相仿。
4、结论
由于linux路由器默认不转发arp报文到,所有若像错误的配置那样配置路由,①号机一直处在询问目的MAC的阶段而无法让路由器④号机转发数据包,所有我们可以通过正确的配置那样配置路由让①号机使用④号机eth1的MAC出去,然后再一步一步转发。或者通过错误的配置那样配置路由,然后在④号机中使用arp代理,从而让①号机获得⑦号机的MAC,从而从发送arp报文阶段到发送ICMP包阶段。
上面就是Linux下利用PC机实现路由转发的方法介绍了,文中也有介绍了错误的设置方法,以防你设置出错,赶紧上手操作看看吧。