JAVA获取机器的MAC地址
需求:获取机器的mac地址
问题:一些国产操作系统获取不到MAC地址,以下搜集了一些获取mac地址的方法
1.获取MAC地址
ps:该方法在银河麒麟中可用
public static String getMAC() throws SocketException {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while(networkInterfaces.hasMoreElements()) {
NetworkInterface network = networkInterfaces.nextElement();
log.info("network:{}", network);
byte[] mac = network.getHardwareAddress();
if(mac == null) {
log.warn("mac is null");
} else {
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 address : {} ", sb);
if (!"".equals(sb.toString())) {
return sb.toString();
}
break;
}
}
return "";
}
2.获取MAC地址
在UOS系统中可用
public static List<String> getMacAddress() {
ArrayList<String> macs = new ArrayList<>();
try {
Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
byte[] mac = null;
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
if (netInterface.isLoopback() || netInterface.isVirtual() || netInterface.isPointToPoint() || !netInterface.isUp()) {
continue;
} else {
mac = netInterface.getHardwareAddress();
if (mac != null) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""));
}
if (sb.length() > 0) {
macs.add(sb.toString().toLowerCase());
}
}
}
}
log.info("获取的mac地址为:{}", macs);
return macs;
} catch (Exception e) {
log.error("MAC地址获取失败", e);
}
return Collections.emptyList();
}
3.获取Linux的MAC地址
public static List<String> getLinuxMACAddress() {
List<String> macs = new ArrayList<>();
BufferedReader bufferedReader = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("ifconfig -a");
bufferedReader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
index = line.toLowerCase().indexOf("硬件地址");
String mac = "";
if (index != -1) {
mac = line.substring(index + 4).trim();
macs.add(mac.toLowerCase());
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}
log.info("MAC address :{}", macs);
return macs;
}
4.获取windows的mac地址
public static List<String> getMACAddressByWindows() throws Exception {
ArrayList<String> rs = new ArrayList<>();
String result = "";
Process process = Runtime.getRuntime().exec("ipconfig /all");
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
int index = -1;
String line;
while ((line = br.readLine()) != null) {
index = line.indexOf("物理地址");
if (index >= 0) {
index = line.indexOf(":");
if (index >= 0) {
result = line.substring(index + 1).trim();
}
rs.add(result.toUpperCase());
}
}
br.close();
log.info("Mac address : {}",rs);
return rs;
}