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");
}