I need to print all the mac addresses of my machine. The recommended way is to use NetworkInterface.getNetworkInterfaces() and iterate on the enumeration returned. However when some devices are down ( NO Ip is configured ) then the above method will not return the interfaces.
Here is the code I wrote for testing purposes :
Enumeration<NetworkInterface> ni = NetworkInterface.getNetworkInterfaces();
while(ni.hasMoreElements()){
NetworkInterface nextElement = ni.nextElement();
byte[] mac = nextElement.getHardwareAddress();
if (mac != null) {
StringBuffer macAddress = new StringBuffer();
for (int i = 0; i < mac.length; i++) {
macAddress.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""));
}
System.out.println(macAddress.toString());
}
}
The output is: 00:03:B2:75:99:C3 (of G1) only.
I do want a pure java solution if possible.
Any thoughts ?
Looks like the "ip addr" shows all network adapters, but not all are configured with an internet address. So, the Java returns only network interfaces, i.e. configured adapters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With