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