package com.billion.main.licenes;
|
|
import java.io.BufferedReader;
|
import java.io.InputStreamReader;
|
|
public class HardwareUtils {
|
|
/**
|
* 获取完整的硬件信息
|
*/
|
public static String getHardwareInfo() {
|
StringBuilder sb = new StringBuilder();
|
try {
|
String cpuId = getCPUID();
|
String motherboardSN = getMotherboardSN();
|
String diskSN = getDiskSN();
|
|
sb.append(cpuId).append("#")
|
.append(motherboardSN).append("#")
|
.append(diskSN);
|
|
return sb.toString();
|
} catch (Exception e) {
|
e.printStackTrace();
|
return null;
|
}
|
}
|
|
/**
|
* 获取CPU序列号
|
*/
|
public static String getCPUID() {
|
String result = "";
|
try {
|
String[] shell = new String[]{};
|
if (isWindows()) {
|
shell = new String[]{"wmic", "cpu", "get", "ProcessorId"};
|
} else if (isLinux()) {
|
shell = new String[]{"bash", "-c", "dmidecode -t processor | grep 'ID' | awk -F ':' '{print $2}' | head -n 1"};
|
}
|
result = executeCommand(shell);
|
if (result.contains("ProcessorId")) {
|
result = result.substring(result.indexOf("ProcessorId") + "ProcessorId".length()).trim();
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return result.trim();
|
}
|
|
/**
|
* 获取主板序列号
|
*/
|
public static String getMotherboardSN() {
|
String result = "";
|
try {
|
String[] shell = new String[]{};
|
if (isWindows()) {
|
shell = new String[]{"wmic", "baseboard", "get", "SerialNumber"};
|
} else if (isLinux()) {
|
shell = new String[]{"bash", "-c", "dmidecode -t baseboard | grep 'Serial Number' | awk -F ':' '{print $2}'"};
|
}
|
result = executeCommand(shell);
|
if (result.contains("SerialNumber")) {
|
result = result.substring(result.indexOf("SerialNumber") + "SerialNumber".length()).trim();
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return result.trim();
|
}
|
|
/**
|
* 获取硬盘序列号
|
*/
|
public static String getDiskSN() {
|
String result = "";
|
try {
|
String[] shell = new String[]{};
|
if (isWindows()) {
|
shell = new String[]{"wmic", "diskdrive", "get", "SerialNumber"};
|
} else if (isLinux()) {
|
shell = new String[]{"bash", "-c", "hdparm -I /dev/sda | grep 'Serial Number' | awk '{print $3}'"};
|
}
|
result = executeCommand(shell);
|
if (result.contains("SerialNumber")) {
|
result = result.substring(result.indexOf("SerialNumber") + "SerialNumber".length()).trim();
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return result.trim();
|
}
|
|
/**
|
* 执行命令行
|
*/
|
private static String executeCommand(String[] command) {
|
StringBuilder result = new StringBuilder();
|
try {
|
Process process = Runtime.getRuntime().exec(command);
|
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
String line;
|
while ((line = reader.readLine()) != null) {
|
result.append(line).append("\n");
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return result.toString();
|
}
|
|
public static void main(String[] args) {
|
System.out.println(getCPUID());
|
}
|
|
/**
|
* 判断是否Windows系统
|
*/
|
private static boolean isWindows() {
|
return System.getProperty("os.name").toLowerCase().contains("windows");
|
}
|
|
/**
|
* 判断是否Linux系统
|
*/
|
private static boolean isLinux() {
|
return System.getProperty("os.name").toLowerCase().contains("linux");
|
}
|
}
|