简体   繁体   English

NetworkInterface不会返回所有mac地址

[英]NetworkInterface doesn't return all mac addresses

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. 在部分代码中,我试图将mac地址与我们的许可证文件绑定在一起,并且我遇到一个问题,即我用于弄清机器所有mac地址的代码未返回所有物理mac地址。 Here is the code to get all the mac addresses 这是获取所有mac地址的代码

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) “ ipconfig / all”的输出显示了这些物理地址(如果需要,我可以粘贴此命令的输出)

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. 现在,如果您看到Java代码没有返回地址“ 00-26-B9-30-6D-C3”,并且还打印了三次地址“ 00:26:B9:30:6D:CB”。 Any idea why this would be happening and how can i fix it. 任何想法为什么会发生这种情况,我该如何解决。

Any help will be deeply appreciated :) 任何帮助将不胜感激:)

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

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