我开发了一个代码,可以让我ping一系列IP地址。 ping扫描的结果标识了本地计算机可以访问的内容,是否可以访问主机名,以及是否无法访问它们。
我在检索可到达的IP地址的MAC地址时遇到了麻烦。有没有人有这个解决方案?
package networkping;
import java.io.IOException;
import java.net.InetAddress;
/**
*
* @author Learner
*/
public class Networkping {
public static void main(String[] args) throws IOException {
InetAddress localhost = InetAddress.getLocalHost();
// this code assumes IPv4 is used
byte[] ip = localhost.getAddress();
for (int i = 1; i <= 254; i++)
{
ip[3] = (byte)i;
InetAddress address = InetAddress.getByAddress(ip);
if (address.isReachable(1000))
{
System.out.println(address + " Address is reachable" );
}
else if (!address.getHostAddress().equals(address.getHostName()))
{
System.out.println(address + " Address is known in a DNS lookup and is reachable ");
}
else
{
System.out.println(address + " Address is unreachable");
}
}
}
由于
答案 0 :(得分:1)
你不能只使用java。
有两种选择:
ARP
。还要查看以下资源/答案:
第二个资源甚至有一个实现的方法,调用名为private String getMac(String ip)