master
hwj 1 year ago
parent 2c8102b872
commit e7f4df4f43
  1. 49
      dntd-tool/src/main/java/com/dky/security/GetCpuInfo.java
  2. 1
      dntd-tool/src/main/java/com/dky/tool/ModelTool.java

@ -1,9 +1,11 @@
package com.dky.security; package com.dky.security;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.InputStream; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.Scanner; import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GetCpuInfo { public class GetCpuInfo {
/** /**
@ -18,32 +20,46 @@ public class GetCpuInfo {
// linux系统用Runtime.getRuntime().exec()执行 dmidecode -t processor 查询cpu序列 // linux系统用Runtime.getRuntime().exec()执行 dmidecode -t processor 查询cpu序列
// windows系统用 wmic cpu get ProcessorId 查看cpu序列 // windows系统用 wmic cpu get ProcessorId 查看cpu序列
if ("LINUX".equals(os)) { if ("LINUX".equals(os)) {
cpuId = getLinuxCpuId("dmidecode -t processor | grep 'ID' | head -n 1", "ID", ":"); cpuId = getLinuxCpuId();
} else { } else {
cpuId = getWindowsCpuId(); cpuId = getWindowsCpuId();
} }
String macK = cpuId.toUpperCase().replace(" ", "");
return cpuId.toUpperCase().replace(" ", ""); macK.replace(":","");
System.out.println(
"本机mac地址:"+macK.replace(":","")
);
return macK.replace(":","");
} }
/** /**
* 获取linux系统CPU序列 * 获取linux系统CPU序列
*/ */
public static String getLinuxCpuId(String cmd, String record, String symbol) throws Exception { public static String getLinuxCpuId() {
String execResult = executeLinuxCmd(cmd); String mac = "";
System.out.println("获取命令执行结果:"+execResult); try
String[] infos = execResult.split("\n"); {
for (String info : infos) { Process p = new ProcessBuilder("ifconfig").start();
info = info.trim(); BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
if (info.indexOf(record) != -1) { String line;
info.replace(" ", ""); while ((line = br.readLine()) != null)
String[] sn = info.split(symbol); {
return sn[1]; Pattern pat = Pattern.compile("\\b\\w+:\\w+:\\w+:\\w+:\\w+:\\w+\\b");
Matcher mat= pat.matcher(line);
if(mat.find())
{
mac=mat.group(0);
}
} }
br.close();
}catch (IOException e) {
e.printStackTrace();
} }
return null; System.out.println("本机mac地址:"+mac);
return mac;
} }
public static String executeLinuxCmd(String cmd){ public static String executeLinuxCmd(String cmd){
try { try {
Runtime run = Runtime.getRuntime(); Runtime run = Runtime.getRuntime();
@ -76,7 +92,8 @@ public class GetCpuInfo {
*/ */
public static String getWindowsCpuId() throws Exception { public static String getWindowsCpuId() throws Exception {
Process process = Runtime.getRuntime().exec( Process process = Runtime.getRuntime().exec(
new String[]{"wmic", "cpu", "get", "ProcessorId"}); new String[]{"wmic", "cpu", "get", "ProcessorId"}
);
process.getOutputStream().close(); process.getOutputStream().close();
Scanner sc = new Scanner(process.getInputStream()); Scanner sc = new Scanner(process.getInputStream());
sc.next(); sc.next();

@ -13,7 +13,6 @@ import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
public class ModelTool { public class ModelTool {
public static List<DevSpec> specList = new ArrayList<>(); public static List<DevSpec> specList = new ArrayList<>();
public static List<DevPrice> priceList = new ArrayList<>(); public static List<DevPrice> priceList = new ArrayList<>();

Loading…
Cancel
Save