【发布时间】:2012-12-27 18:18:09
【问题描述】:
我需要获取连接到我的家庭网络的所有远程计算机的 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
【问题讨论】:
-
出于什么目的?在 Java 中,除了显示 MAC 地址之外,您没有任何用处。
标签: java ip mac-address