【发布时间】:2013-12-09 23:06:39
【问题描述】:
我开发了一个代码,使我能够 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");
}
}
}
谢谢
【问题讨论】:
-
遗憾的是,如果没有 JNI,Java 无法做到这一点,并且取决于网络配置/操作系统,可能根本不会。
-
@hexafraction 感谢您的评论和意见,但我一直将此作为作业,导师向我保证可以这样做
-
@user3071069 看看我回答中的链接。这是可能的,但你需要绕道而行。