0

我有在java中读取MAC地址的功能:

    for(Enumeration<NetworkInterface> enm = NetworkInterface.getNetworkInterfaces(); enm.hasMoreElements();){
        NetworkInterface network = (NetworkInterface) enm.nextElement();
          if(null != network.getHardwareAddress()){
              System.out.print(network.getDisplayName());
              byte[] mac = network.getHardwareAddress();
              StringBuilder sb = new StringBuilder(18);
              for (byte b : mac) {
                  if (sb.length() > 0)
                      sb.append(':');
                  sb.append(String.format("%02x", b & 0xff));
              }                  

              System.out.print(sb.toString());
              return null; 
          }
       }

这个结果: wlan0: 00:25:d3:9c:ad:7a 但是当我这样做时:iwconfig,我收到:00:0D:F3:0D:DD:DC for wlan0

问题,为什么?

编辑:对不起,当我运行 ifconfig(不是 iwconfig)时,它会响应:

wlan0 Link encap:Ethernet HWaddr 00:25:d3:9c:ad:7a
inet addr:192.168.50.100 Bcast:192.168.50.255 Mask:255.255.255.0 inet6 addr: fe80::225:d3ff:fe9c:ad7a/64 范围: Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX 数据包:2417546 错误:0 丢弃:0 超限:0 帧:0 TX 数据包:1608679 错误:0 丢弃:0 超限:0 运营商:0 冲突:0 txqueuelen:1000 RX字节:3099222889 (3.0 GB) 发送字节:183978636 (183.9 MB)

所以没关系,还是谢谢你。

4

1 回答 1

0

尝试运行此代码:

    for (Enumeration<NetworkInterface> enm = NetworkInterface
            .getNetworkInterfaces(); enm.hasMoreElements();) {
        NetworkInterface network = (NetworkInterface) enm.nextElement();
        if (null != network.getHardwareAddress()) {
            System.out.print(network.getDisplayName() + " is ");
            byte[] mac = network.getHardwareAddress();
            StringBuilder sb = new StringBuilder(18);
            for (byte b : mac) {
                if (sb.length() > 0)
                    sb.append(':');
                sb.append(String.format("%02x", b & 0xff));
            }

            System.out.println(sb.toString());
        }
    }

这将打印出所有网络接口的名称/mac 地址,您的旧代码只是第一个。查看这些 MAC 地址是否与 iwconfig 给出的地址匹配

于 2012-09-08T15:52:11.360 回答