NetworkInterface doesn't return all mac addresses

百般思念 提交于 2019-12-11 02:38:41

问题


In part of our code i am trying to bind a mac address with our license file and i am facing an issue that my code for figuring out all the mac addresses of the machine is not returning all the physical mac addresses. Here is the code to get all the mac addresses

public static Collection<String> getAllLocalMacAddresses() throws IOException {
    final Enumeration<NetworkInterface> inetAddresses = NetworkInterface.getNetworkInterfaces();
    final Collection<String> addresses = new LinkedList<String>();

    while(inetAddresses.hasMoreElements()){
        final byte[] macBytes = inetAddresses.nextElement().getHardwareAddress();

        if(macBytes == null)
            continue;

        addresses.add(getMacAddress(macBytes));
    }

    return addresses;
}

static String getMacAddress(byte[] macBytes){
    final StringBuilder strBuilder = new StringBuilder();

    for (int i = 0; i < macBytes.length; i++){
        strBuilder.append(String.format("%02X%s", macBytes[i],
                (i < macBytes.length - 1) ? ":" : ""));
    }

    return strBuilder.toString().toUpperCase();
}

Here is the result that i get from this method

[, 00:00:00:00:00:00:00:E0, 00:26:B9:30:6D:CB, 00:26:B9:30:6D:CB, 00:00:00:00:00:00:00:E0, 00:26:B9:30:6D:CB, 02:26:B9:30:6D:C1, 00:26:B9:30:6D:CB, 00:00:00:00:00:00:00:E0]

The output from "ipconfig /all" shows me these physical addresses (i could paste the out put for this command if required)

00-26-B9-30-6D-C3
02-26-B9-30-6D-C1
00-26-B9-30-6D-C5
00-26-B9-30-6D-C9
00-26-B9-30-6D-CB
00-00-00-00-00-00-00-E0

Now if you see the java code did not return the address "00-26-B9-30-6D-C3" and also printed the address "00:26:B9:30:6D:CB" thrice. Any idea why this would be happening and how can i fix it.

Any help will be deeply appreciated :)


回答1:


Try http://docs.oracle.com/javase/6/docs/api/java/net/NetworkInterface.html#getSubInterfaces() - it could be a virtual interface



来源:https://stackoverflow.com/questions/18450460/networkinterface-doesnt-return-all-mac-addresses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!