简体   繁体   中英

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

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

you can use NetworkInterface.getHardwareAddress . You can get the list of all network cards with NetworkInterface.getNetworkInterfaces() .

you can use this code for knowing MAC Address.

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):

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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