日期:2014-05-16  浏览次数:20708 次

linux arping命令学习

arping命令用来向邻近的主机发生ARP REQUEST数据包。

1. arping命令可以用来测试局域网各个主机之间的连通性,不能用于测试其是否能与互联网连通,
sh-# ping
www.google.com
PING www.google.com (74.125.239.147) 56(84) bytes of data.
64 bytes from nuq05s02-in-f19.1e100.net (74.125.239.147): icmp_req=1 ttl=53 time=267 ms
64 bytes from nuq05s02-in-f19.1e100.net (74.125.239.147): icmp_req=2 ttl=53 time=269 ms
64 bytes from nuq05s02-in-f19.1e100.net (74.125.239.147): icmp_req=3 ttl=53 time=289 ms
^C
---
www.google.com ping statistics ---
4 packets transmitted, 3 received, 25% packet loss, time 3011ms
rtt min/avg/max/mdev = 267.321/275.337/289.108/9.790 ms

sh-# arping -I eth0 www.google.com -w 5
ARPING 74.125.239.147 from 192.168.0.153 eth0
Sent 6 probes (6 broadcast(s))
Received 0 response(s)
sh-# arping -I eth0 192.168.0.151 -w 5
ARPING 192.168.0.151 from 192.168.0.153 eth0
Unicast reply from 192.168.0.151 [78:52:1A:4D:D0:A5]  56.882ms
Unicast reply from 192.168.0.151 [78:52:1A:4D:D0:A5]  280.078ms
Unicast reply from 192.168.0.151 [78:52:1A:4D:D0:A5]  92.872ms
Unicast reply from 192.168.0.151 [78:52:1A:4D:D0:A5]  116.720ms
Unicast reply from 192.168.0.151 [78:52:1A:4D:D0:A5]  129.921ms
Unicast reply from 192.168.0.151 [78:52:1A:4D:D0:A5]  48.195ms
Sent 6 probes (1 broadcast(s))
Received 6 response(s)

2. arping命令可以用来测试局域网中某个特定的IP地址是否已经被占用。
我们知道局域网中如果IP地址有冲突可能会带来各种奇怪的网络问题,所以arping命令在手动设定IP地址时
会非常有用。可以在设定IP地址之前,使用arping命令进行测试。
利用arping命令执行的返回码来确认执行结果:
如果返回结果为1,说明局域网中已经存在该IP地址了;
如果返回结果为0,说明局域网中暂时无人使用该IP地址,那我们就可以使用了。
sh-# arping -I eth0 -D 192.168.0.151 -w 5
ARPING 192.168.0.151 from 0.0.0.0 eth0
Unicast reply from 192.168.0.151 [78:52:1A:4D:D0:A5]  141.406ms
Sent 1 probes (1 broadcast(s))
Received 1 response(s)
sh-# echo $?
1
sh-# arping -I eth0 -D 192.168.0.152 -w 5
ARPING 192.168.0.152 from 0.0.0.0 eth0
Sent 6 probes (6 broadcast(s))
Received 0 response(s)
sh-# echo $?
0
sh-#

问题:如果你的C应用程序需要通过检查IP地址是否可用来决定用户界面如何显示,要怎么做?
这就要借助于linux提供的库函数system函数了。
#include <stdlib.h>
if (0 == system("arping -I eth0 -D 192.168.10.123 -w 5"))
{
    printf("\nip not exist, can use this ip\n");
}
else
{
    printf("\nip exist, can not use this ip\n");
}

不过system函数是一个同步函数,它可能会卡住用户操作。关于system函数的更多信息,请参考下面的博文:
http://blog.csdn.net/boyxulin1986/article/details/10962319

 

1楼boyxulin19863天前 12:26
自己必须顶自己