NetworkInterface不会返回所有mac地址 [英] NetworkInterface doesn't return all mac addresses

查看:161
本文介绍了NetworkInterface不会返回所有mac地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的部分代码中,我试图将Mac地址与我们的许可证文件绑定,并且遇到一个问题,即我用于弄清机器所有mac地址的代码未返回所有物理mac地址.这是获取所有mac地址的代码

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]

"ipconfig/all"的输出显示了这些物理地址(如果需要,我可以粘贴此命令的输出)

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

现在,如果您看到Java代码没有返回地址"00-26-B9-30-6D-C3",并且还打印了三次地址"00:26:B9:30:6D:CB".任何想法为什么会发生这种情况,我该如何解决.

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

推荐答案

尝试

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

这篇关于NetworkInterface不会返回所有mac地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆