1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
Enumeration<NetworkInterface> networkInterface = NetworkInterface.getNetworkInterfaces();
// iterate over all interfaces
while (networkInterface.hasMoreElements()) {
// get an interface
NetworkInterface network = networkInterface.nextElement();
// get its hardware or mac address
byte[] macAddressBytes = network.getHardwareAddress();
if (macAddressBytes != null) {
System.out.print("MAC address of interface \""
+ network.getName() + "\" is : ");
// initialize a string builder to hold mac address
StringBuilder macAddressStr = new StringBuilder();
// iterate over the bytes of mac address
for (int i = 0; i < macAddressBytes.length; i++) {
// convert byte to string in hexadecimal form
macAddressStr.append(String.format("%02X",
macAddressBytes[i]));
// check if there are more bytes, then add a "-" to make
// it more readable
if (i < macAddressBytes.length - 1)
{
macAddressStr.append("-");
}
}
System.out.println(macAddressStr.toString());
} |
Partager