public static void getAllMacAdress() { Enumeration<NetworkInterface> netInterfaces = null; try { // 获得所有网络接口 netInterfaces = NetworkInterfacegetNetworkInterfaces(); while (netInterfaceshasMoreElements()) { Systemout println("=============================================="); String mac = ""; StringBuffer sb = new StringBuffer(); NetworkInterface ni = netInterfacesnextElement(); Systemoutprintln("DisplayName: " + nigetDisplayName()); Systemoutprintln("Name: " + nigetName()); byte[] macs = nigetHardwareAddress(); // 该interface不存在HardwareAddress继续下一次循环 if (macs == null) { continue; } for (int i = ; i < macslength; i++) { mac = IntegertoHexString(macs[i] & xFF); if (maclength() == ) { mac = + mac; } sbappend(mac + ""); } mac = sbtoString(); mac = macsubstring( maclength() ); Systemoutprintln(mac); Enumeration<InetAddress> ips = nigetInetAddresses(); while (ipshasMoreElements()) { Systemoutprintln("IP: " + ipsnextElement()getHostAddress()); } } } catch (SocketException e) { eprintStackTrace(); } }
本方法需要使用使用jdk