可能是重复内容:
查询ARP缓存以获取MAC ID
我需要获取连接到我家庭网络的所有远程机器的MAC地址。我可以使用以下代码读取我的机器的MAC地址,但我不知道如何获取网络上其他机器的MAC地址。是否可能实现?
import java.net.InetAddress;
import java.net.NetworkInterface;
class Test {
public static void main(String[] args) throws Exception {
InetAddress address = InetAddress.getLocalHost();
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
byte[] mac = ni.getHardwareAddress();
for (int i = 0; i < mac.length; i++) {
System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
}
}
}
输出(MyComputer的6字节mac地址)
30-60-77-12-cd-99