我开发了一个代码,使我能够 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");
}
}
}
谢谢
最佳答案
仅使用 java 无法做到这一点。
有两个选项:
- 通过 java 执行另一个进程,并通过标准输出与原始 java 应用程序进行通信。例如,这样的过程就是
ARP
。 - 按照评论中的建议使用 JNI。
另请查看以下资源/答案:
第二个资源甚至有一个调用 ARP 的实现方法,名为 private String getMac(String ip)
关于java - 从 PING 扫描中检索 MAC 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20482748/