简体   繁体   English

如何在不使用 Java 中的 IP 的情况下获取 MAC 地址?

[英]How can I get MAC Address without using the IP in Java?

如何在不知道和使用Java中的IP地址的情况下找到网卡的MAC地址?

you can use NetworkInterface.getHardwareAddress .您可以使用NetworkInterface.getHardwareAddress You can get the list of all network cards with NetworkInterface.getNetworkInterfaces() .您可以使用NetworkInterface.getNetworkInterfaces()获取所有网卡的列表。

you can use this code for knowing MAC Address.您可以使用此代码来了解 MAC 地址。

InetAddress address = InetAddress.getLocalHost();
NetworkInterface nwi = NetworkInterface.getByInetAddress(address);
byte mac[] = nwi.getHardwareAddress();
System.out.println(mac);

You can use system command like below(if you on nix systems, if your os is windows use ipconfig in a similar way):您可以使用如下系统命令(如果您在nix系统上,如果您的操作系统是 windows 以类似的方式使用ipconfig ):

Runtime r = Runtime.getRuntime();
Process p = r.exec("ifconfig -u |grep ether");
p.waitFor();
BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";

while ((line = b.readLine()) != null) {
  System.out.println(line);
}

b.close();

On my system it gives:在我的系统上它给出:

ether 28:37:37:15:3b:31 
ether b2:00:10:65:56:41 
...

Notice that there are maybe many MAC addresses, the Bluetooth, the Ethernet, the WIFI and so on.请注意,可能有很多 MAC 地址、蓝牙、以太网、WIFI 等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM