Java - 获取Linux系统的MAC地址

时间:2011-07-06 11:13:51

标签: java macos crash mac-address

我正在尝试使用此代码获取Linux系统的MAC地址:

try {
  ip = InetAddress.getLocalHost();
  NetworkInterface network = NetworkInterface.getByInetAddress(ip);
  byte[] mac = network.getHardwareAddress();
  // System.out.print("Current MAC address: ");
  for (int i = 0; i < mac.length; i++) {
    is = is + Integer.parseInt(
      String.format("%02X%s", mac[i], (i < mac.length - 1) ? "" : ""),16);
  }
} catch (UnknownHostException e) {
  e.printStackTrace();
} catch (SocketException e) {
  e.printStackTrace();
}

但它只是崩溃了...有谁知道为什么?

3 个答案:

答案 0 :(得分:6)

您可能有多个网络接口,我不会指望接口的名称。我建议你查看所有接口,找一个有MAC地址的接口。 您可以将此示例用作基线:

try {

        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while(networkInterfaces.hasMoreElements())
        {
            NetworkInterface network = networkInterfaces.nextElement();
            System.out.println("network : " + network);
            byte[] mac = network.getHardwareAddress();
            if(mac == null)
            {
                System.out.println("null mac");             
            }
            else
            {
                System.out.print("MAC address : ");

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < mac.length; i++)
                {
                    sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));        
                }
                System.out.println(sb.toString());  
                break;
            }
        }
    } catch (SocketException e){

        e.printStackTrace();

    }

答案 1 :(得分:3)

从您的评论中,network显然是null,这意味着getByInetAddress()无法找到具有该IP地址的接口(请参阅JavaDocs:http://download.oracle.com/javase/1.5.0/docs/api/java/net/NetworkInterface.html#getByInetAddress(java.net.InetAddress))。

答案 2 :(得分:2)

只是对盖伊代码的一个小修改:

public String searchForMac() throws SocketException {
    String firstInterface = null;        
    Map<String, String> addressByNetwork = new HashMap<>();
    Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();

    while(networkInterfaces.hasMoreElements()){
        NetworkInterface network = networkInterfaces.nextElement();

        byte[] bmac = network.getHardwareAddress();
        if(bmac != null){
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < bmac.length; i++){
                sb.append(String.format("%02X%s", bmac[i], (i < bmac.length - 1) ? "-" : ""));        
            }

            if(sb.toString().isEmpty()==false){
                addressByNetwork.put(network.getName(), sb.toString());
                System.out.println("Address = "+sb.toString()+" @ ["+network.getName()+"] "+network.getDisplayName());
            }

            if(sb.toString().isEmpty()==false && firstInterface == null){
                firstInterface = network.getName();
            }
        }
    }

    if(firstInterface != null){
        return addressByNetwork.get(firstInterface);
    }

    return null;
}

此代码已经在Windows,Linux(Ubuntu)和Mac OS X上测试成功。

因为网络可以为null,所以我忽略所有空的情况,并且我也忽略空地址。我想如果我们不这样做,我们会看到崩溃。我选择了第一个找到的地址并且它有效,但可能是错误的,所以只需测试一下。