-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSystemUtil.java
359 lines (333 loc) · 10 KB
/
SystemUtil.java
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package cn.xanderye.util;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* Created on 2020/11/5.
*
* @author XanderYe
*/
@Slf4j
public class SystemUtil {
/**
* 空格
*/
public static final String BREAK = " ";
/**
* Tab
*/
public static final String TAB = " ";
/**
* Windows换行符
*/
public static final String WINDOWS_LINE_BREAK = "\r\n";
/**
* UNIX换行符
*/
public static final String UNIX_LINE_BREAK = "\r";
/**
* 注册表反馈
*/
public static final String[] REG_RESULT = new String[]{"操作成功完成。"};
/**
* Ip正则
*/
public static final Pattern IP_PATTERN = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
/**
* 本地Ip
*/
public static final String LOCALHOST_IP = "127.0.0.1";
/**
* 调用cmd方法,默认GBK编码
* @param cmdStr
* @return java.lang.String
* @author XanderYe
* @date 2020/11/5
*/
public static String execStr(String cmdStr) {
return execStr(getCharset(), cmdStr);
}
/**
* 异步掉用cmd方法,默认GBK编码
* @param consumer 自由实现日志打印
* @param cmdStr
* @return void
* @author XanderYe
* @date 2021/4/18
*/
public static void execStrAsync(Consumer<String> consumer, String cmdStr) {
execStrAsync(getCharset(), consumer, cmdStr);
}
/**
* 调用cmd方法,默认GBK编码
* @param cmds
* @return java.lang.String
* @author XanderYe
* @date 2020/11/5
*/
public static String execStr(String...cmds) {
return execStr(getCharset(), cmds);
}
/**
* 异步调用cmd方法,默认GBK编码
* @param cmds
* @return java.lang.String
* @author XanderYe
* @date 2020/11/5
*/
public static void execStrAsync(Consumer<String> consumer, String...cmds) {
execStrAsync(getCharset(), consumer, cmds);
}
/**
* 调用cmd方法,同步返回结果
* @param charset
* @param cmds
* @return java.lang.String
* @author XanderYe
* @date 2020/11/5
*/
public static String execStr(Charset charset, String...cmds) {
if (1 == cmds.length) {
if (cmds[0] == null || "".equals(cmds[0])) {
throw new RuntimeException("Empty command !");
}
cmds = cmds[0].split(BREAK);
}
Process process = null;
try {
process = new ProcessBuilder(cmds).redirectErrorStream(true).start();
InputStream is = process.getInputStream();
BufferedReader buffer = new BufferedReader(new InputStreamReader(is, charset));
String line;
StringBuilder sb = new StringBuilder();
while ((line = buffer.readLine()) != null) {
sb.append(line).append(getLineBreak());
}
is.close();
return sb.toString().trim();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != process) {
process.destroy();
}
}
return null;
}
/**
* 调用cmd,异步打印结果
* @param charset
* @param consumer 自由实现日志打印
* @param cmds
* @return void
* @author XanderYe
* @date 2021/4/18
*/
public static void execStrAsync(Charset charset, Consumer<String> consumer, String...cmds) {
if (1 == cmds.length) {
if (cmds[0] == null || "".equals(cmds[0])) {
throw new RuntimeException("Empty command !");
}
cmds = cmds[0].split(BREAK);
}
try {
Process process = new ProcessBuilder(cmds).redirectErrorStream(true).start();
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.execute(() -> {
try {
InputStream is = process.getInputStream();
BufferedReader buffer = new BufferedReader(new InputStreamReader(is, charset));
String line;
while ((line = buffer.readLine()) != null) {
if (consumer != null) {
consumer.accept(line);
} else {
// 默认打印日志
log.info(line);
}
}
is.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
process.destroy();
}
});
executorService.shutdown();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取系统Ip地址
* @param
* @return java.util.List<java.lang.String>
* @author XanderYe
* @date 2020/12/18
*/
public static List<String> getSystemIp() {
if (isWindows()) {
return getWindowsIp();
} else {
return getLinuxIp();
}
}
/**
* 获取Windows下的Ip地址
* @param
* @return java.util.List<java.lang.String>
* @author XanderYe
* @date 2020/12/18
*/
private static List<String> getWindowsIp() {
String res = execStr("ipconfig");
List<String> rowList = Arrays.asList(res.split(getLineBreak()));
List<String> ipList = new ArrayList<>();
for (String string : rowList) {
if(string.contains("IPv4 地址") || string.contains("IPv4 Address")){
Matcher mc = IP_PATTERN.matcher(string);
if(mc.find()){
ipList.add(mc.group());
}
}
}
return ipList;
}
/**
* 获取Linux下的Ip地址
* @param
* @return java.util.List<java.lang.String>
* @author XanderYe
* @date 2020/12/18
*/
private static List<String> getLinuxIp() {
String res = execStr("ip addr");
List<String> rowList = Arrays.asList(res.split(getLineBreak()));
List<String> ipList = new ArrayList<>();
for (String string : rowList) {
if(string.contains("inet")){
Matcher mc = IP_PATTERN.matcher(string);
if(mc.find()){
String ip = mc.group();
if (!LOCALHOST_IP.equals(ip)) {
ipList.add(mc.group());
}
}
}
}
return ipList;
}
/**
* 获取CpuId
* @param
* @return java.lang.String
* @author XanderYe
* @date 2021/1/22
*/
public static List<String> getCpuId() {
if (isWindows()) {
return getWindowsCpuId();
} else {
return getLinuxCpuId();
}
}
/**
* Windows下获取CpuId
* @param
* @return java.lang.String
* @author XanderYe
* @date 2021/1/22
*/
private static List<String> getWindowsCpuId() {
String res = SystemUtil.execStr("wmic", "cpu", "get", "ProcessorId");
String[] strs = res.split("(\r|\n|\r\n)");
return Arrays.stream(strs)
.filter(str -> !str.contains("ProcessorId") && str.length() > 0)
.map(String::trim)
.collect(Collectors.toList());
}
/**
* Linux下获取CpuId
* @param
* @return java.lang.String
* @author XanderYe
* @date 2021/1/22
*/
private static List<String> getLinuxCpuId() {
String res = execStr("sh", "-c", "dmidecode -t processor | grep 'ID'");
String[] strs = res.split("(\r|\n|\r\n)");
return Arrays.stream(strs)
.map(str -> res.substring(res.indexOf(":") + 1).replace(" ", "").trim())
.collect(Collectors.toList());
}
/**
* 增加注册表
* @param path 注册表路径
* @param key 键
* @param type 类型 常用 REG_SZ/REG_DWORD
* @param value 值
* @return boolean
* @author XanderYe
* @date 2020/11/15
*/
public static boolean addReg(String path, String key, String type, String value) {
String command = "reg add \"" + path + "\" /v " + key + " /t " + type + " /d " + value + " /f";
String res = execStr(command).replaceAll("[\\r|\\n|\\\\s]", "");
return Arrays.asList(REG_RESULT).contains(res);
}
/**
* 删除注册表
* @param path 注册表路径
* @param key 键
* @return boolean
* @author XanderYe
* @date 2020/11/15
*/
public static boolean deleteReg(String path, String key) {
String command = "reg delete \"" + path + "\" /v " + key + " /f";
String res = execStr(command).replaceAll("[\\r|\\n|\\\\s]", "");
return Arrays.asList(REG_RESULT).contains(res);
}
/**
* 判断系统环境
* @param
* @return boolean
* @author XanderYe
* @date 2020/11/5
*/
public static boolean isWindows() {
return System.getProperty("os.name").toLowerCase().contains("windows");
}
/**
* 获取系统字符编码
* @param
* @return java.nio.charset.Charset
* @author XanderYe
* @date 2020/11/5
*/
public static Charset getCharset() {
return isWindows() ? Charset.forName("GBK") : Charset.defaultCharset();
}
/**
* 获取系统换行符
* @param
* @return java.lang.String
* @author XanderYe
* @date 2020/11/5
*/
public static String getLineBreak() {
return isWindows() ? WINDOWS_LINE_BREAK : UNIX_LINE_BREAK;
}
}