使用Java在本地计算机上获取MAC地址

我可以用

ip = InetAddress.getLocalHost(); NetworkInterface.getByInetAddress(ip); 

获取mac地址,但是如果我在离线机器中使用此代码则不起作用。

那么, 我怎样才能获得Mac地址?

使用Java 6+,您可以使用NetworkInterface.getHardwareAddress

请记住,计算机可以没有网卡,尤其是嵌入式或虚拟网卡。 它也可以有多个。 您可以使用NetworkInterface.getNetworkInterfaces()获取所有网卡的列表。

通过我在这里找到的所有可能的解决方案和另一个回复,我将为我的解决方案做出贡献。 您需要使用包含“ip”或“mac”的String指定参数,具体取决于您需要检查的内容。 如果计算机没有接口,那么它将返回一个包含null的String,否则将返回一个包含你要求的字符串(ip地址或mac)。

如何使用它:

 System.out.println("Ip: " + GetNetworkAddress.GetAddress("ip")); System.out.println("Mac: " + GetNetworkAddress.GetAddress("mac")); 

结果(如果计算机有网卡):

 Ip: 192.168.0.10 Mac: 0D-73-ED-0A-27-44 

结果(如果计算机没有网卡):

 Ip: null Mac: null 

这是代码:

 import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Enumeration; public class GetNetworkAddress { public static String GetAddress(String addressType) { String address = ""; InetAddress lanIp = null; try { String ipAddress = null; Enumeration net = null; net = NetworkInterface.getNetworkInterfaces(); while (net.hasMoreElements()) { NetworkInterface element = net.nextElement(); Enumeration addresses = element.getInetAddresses(); while (addresses.hasMoreElements() && element.getHardwareAddress().length > 0 && !isVMMac(element.getHardwareAddress())) { InetAddress ip = addresses.nextElement(); if (ip instanceof Inet4Address) { if (ip.isSiteLocalAddress()) { ipAddress = ip.getHostAddress(); lanIp = InetAddress.getByName(ipAddress); } } } } if (lanIp == null) return null; if (addressType.equals("ip")) { address = lanIp.toString().replaceAll("^/+", ""); } else if (addressType.equals("mac")) { address = getMacAddress(lanIp); } else { throw new Exception("Specify \"ip\" or \"mac\""); } } catch (UnknownHostException ex) { ex.printStackTrace(); } catch (SocketException ex) { ex.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } return address; } private static String getMacAddress(InetAddress ip) { String address = null; try { NetworkInterface network = NetworkInterface.getByInetAddress(ip); byte[] mac = network.getHardwareAddress(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < mac.length; i++) { sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "")); } address = sb.toString(); } catch (SocketException ex) { ex.printStackTrace(); } return address; } private static boolean isVMMac(byte[] mac) { if(null == mac) return false; byte invalidMacs[][] = { {0x00, 0x05, 0x69}, //VMWare {0x00, 0x1C, 0x14}, //VMWare {0x00, 0x0C, 0x29}, //VMWare {0x00, 0x50, 0x56}, //VMWare {0x08, 0x00, 0x27}, //Virtualbox {0x0A, 0x00, 0x27}, //Virtualbox {0x00, 0x03, (byte)0xFF}, //Virtual-PC {0x00, 0x15, 0x5D} //Hyper-V }; for (byte[] invalid: invalidMacs){ if (invalid[0] == mac[0] && invalid[1] == mac[1] && invalid[2] == mac[2]) return true; } return false; } } 

更新时间02/05/2017:感谢@mateuscb在post中如何确定Java中的Internet网络接口 ,遗憾的是之前没有在该post上获得任何upvote,但他为此更新做出了贡献。

该方法已被改进,以跳过虚拟机网卡(最流行的VM软件)

至于计算机处于脱机状态,它通常没有分配IP,因为DHCP被广泛使用……

对于标题中的问题: NetworkInterface.getHardwareAddress()

另一种方法是通过本机代码执行使用OS命令’getmac’。

  Process p = Runtime.getRuntime().exec("getmac /fo csv /nh"); java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream())); String line; line = in.readLine(); String[] result = line.split(","); System.out.println(result[0].replace('"', ' ').trim()); 

尝试这个:

 final NetworkInterface netInf = NetworkInterface.getNetworkInterfaces().nextElement(); final byte[] mac = netInf.getHardwareAddress(); final StringBuilder sb = new StringBuilder(); for (int i = 0; i < mac.length; i++) { sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "")); } log.info("Mac addr: {}", sb.toString()); 

从这里清理代码:

 import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; public class HardwareAddress { public static String getMacAddress() throws UnknownHostException, SocketException { InetAddress ipAddress = InetAddress.getLocalHost(); NetworkInterface networkInterface = NetworkInterface .getByInetAddress(ipAddress); byte[] macAddressBytes = networkInterface.getHardwareAddress(); StringBuilder macAddressBuilder = new StringBuilder(); for (int macAddressByteIndex = 0; macAddressByteIndex < macAddressBytes.length; macAddressByteIndex++) { String macAddressHexByte = String.format("%02X", macAddressBytes[macAddressByteIndex]); macAddressBuilder.append(macAddressHexByte); if (macAddressByteIndex != macAddressBytes.length - 1) { macAddressBuilder.append(":"); } } return macAddressBuilder.toString(); } } 

@Jesus Flores,仍然没有为我工作,这段代码:

 element.getHardwareAddress().length > 0 

抛出exception。 我们必须检查是否为null。 您的案例中的最终解决方案对我有用:

  while (addresses.hasMoreElements() && element.getHardwareAddress() != null && element.getHardwareAddress().length > 0 && !isVMMac(element.getHardwareAddress())) { final InetAddress ip = addresses.nextElement(); if (ip instanceof Inet4Address) { // || ip instanceof Inet6Address if (ip.isSiteLocalAddress()) { ipAddress = ip.getHostAddress(); lanIp = InetAddress.getByName(ipAddress); } } } 

我一直在wi-fi工作。

 public static void main(String[] args){ InetAddress ip; try { ip = InetAddress.getLocalHost(); System.out.println("Current IP address : " + ip.getHostAddress()); NetworkInterface network = NetworkInterface.getByInetAddress(ip); byte[] mac = network.getHardwareAddress(); System.out.print("Current 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()); } catch (Exception e) { e.printStackTrace(); } } 

输出:

 Current IP address : 192.168.21.60 Current MAC address : 70-5A-0F-3C-84-F2