admin
2025-03-29 9cd0e111d7315074f5574558dab3cae087ff1f68
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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");
    }
}