1.判断操作系统是Windows还是Linux
private static Boolean isLinux() {
String os = System.getProperty("os.name");
log.info("os.name: {}", os);
return !os.toLowerCase().startsWith("win");
}
2. Linux:
获取MAC地址
private static String getMACAddressByLinux() throws Exception {
String[] cmd = {"ifconfig"};
Process process = Runtime.getRuntime().exec(cmd);
process.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
String str1 = sb.toString();
String str2 = str1.split("ether")[1].trim();
String result = str2.split("txqueuelen")[0].trim();
log.info("Linux MacAddress is: {}", result);
br.close();
return result;
}
获取硬盘序列号
private static String getIdentifierByLinux() throws Exception {
String[] cmd = {"fdisk", "-l"};
Process process = Runtime.getRuntime().exec(cmd);
process.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
String str1 = sb.toString();
String str2 = str1.split("identifier:")[1].trim();
String result = str2.split("Device Boot")[0].trim();
log.info("Linux Identifier is: {}", result);
br.close();
return result;
}
3. Windows:
获取MAC地址: (默认获取第一张网卡)
private static String getMACAddressByWindows() throws Exception {
String result = "";
Process process = Runtime.getRuntime().exec("ipconfig /all");
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
String line;
int index = -1;
while ((line = br.readLine()) != null) {
index = line.toLowerCase().indexOf("物理地址");
if (index >= 0) {// 找到了
index = line.indexOf(":");
if (index >= 0) {
result = line.substring(index + 1).trim();
}
break;
}
}
log.info("Windows MACAddress is: {}", result);
br.close();
return result;
}
获取硬盘序列号: (默认获取C盘)
private static String getIdentifierByWindows() throws Exception {
String result = "";
Process process = Runtime.getRuntime().exec("cmd /c dir C:");
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("卷的序列号是 ") != -1) {
result = line.substring(line.indexOf("卷的序列号是 ") + "卷的序列号是 ".length(), line.length());
break;
}
}
log.info("Windows Identifier is: {}", result);
br.close();
return result;
}
4. 测试:
public static void main(String[] a) throws Exception {
// 判断是Linux还是Windows
if (isLinux()) {
// Linux操作系统
String macAddress = getMACAddressByLinux();
System.out.println("Linux macAddress: " + macAddress);
String Identifier = getIdentifierByLinux();
System.out.println("Linux Identifier: " + Identifier);
} else {
// Windows操作系统
String macAddress = getMACAddressByWindows();
System.out.println("Windows macAddress: " + macAddress);
String Identifier = getIdentifierByWindows();
System.out.println("Windows Identifier: " + Identifier);
}
}
注意事项:
在Windows环境使用javac Test.java 命令编译该java文件时, 需指定编码, 应使用以下命令:
javac -encoding UTF-8 Test.java