package com.whyc.util;
|
|
import java.io.BufferedReader;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.InputStreamReader;
|
import java.util.Locale;
|
import java.util.Scanner;
|
|
/**
|
* 电脑 CPU 主板 硬盘 序列号获取工具
|
*/
|
public class SerialNumberUtil {
|
|
public static String getSerialNumber(){
|
return getCPUNumber()+getBoisNumber()+getDiskDriveSerialNumber();
|
}
|
|
public static String getCPUNumber(){
|
String os = System.getProperty("os.name").toLowerCase();
|
//System.out.println(os);
|
Process process = null;
|
String serial = null;
|
if(os.contains("window")) {
|
//System.out.println("windows");
|
try {
|
process = Runtime.getRuntime().exec(
|
new String[]{"wmic", "cpu", "get", "ProcessorId"});
|
process.getOutputStream().close();
|
|
Scanner sc = new Scanner(process.getInputStream());
|
|
String property = sc.next();
|
|
serial = sc.next();
|
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}else{
|
//linux
|
//System.out.println("linux");
|
serial =getSerialNumber("dmidecode -t processor | grep 'ID'", "ID",":");
|
}
|
|
return serial;
|
|
}
|
|
/**
|
* 集成在主板Bios上的操作系统UUID
|
* @return
|
*/
|
public static String getBoisNumber(){
|
String os = System.getProperty("os.name").toLowerCase();
|
Process process = null;
|
String serial = null;
|
if(os.contains("window")) {
|
try {
|
process = Runtime.getRuntime().exec(
|
new String[]{"wmic", "csproduct", "get", "UUID"});
|
process.getOutputStream().close();
|
|
Scanner sc = new Scanner(process.getInputStream());
|
|
String property = sc.next();
|
|
serial = sc.next();
|
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}else {
|
//linux
|
serial =getSerialNumber("dmidecode |grep 'Serial Number'", "Serial Number",":"); }
|
|
return serial;
|
|
}
|
|
public static String getDiskDriveSerialNumber(){
|
String os = System.getProperty("os.name").toLowerCase();
|
Process process = null;
|
String serial = "";
|
if(os.contains("window")) {
|
try {
|
process = Runtime.getRuntime().exec(
|
new String[]{"wmic", "diskdrive", "get", "serialnumber"});
|
process.getOutputStream().close();
|
|
|
BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
String line;
|
int count = 0;
|
while ((line = input.readLine()) != null) {
|
count++;
|
if (count != 1) {
|
line = line.trim();
|
serial += line;
|
}
|
}
|
input.close();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}else {
|
//linux
|
//中英文环境下,fdisk命令不同的结果,一个是中文,一个是英文名
|
Locale locale = Locale.getDefault();
|
if((locale.getLanguage().equals("zh"))){
|
serial=getSerialNumber("fdisk -l", "磁盘标识符",":");
|
}else{ //不是中文,则采用en_US
|
serial=getSerialNumber("fdisk -l", "Disk identifier",":");
|
}
|
}
|
return serial;
|
}
|
|
/**linux*/
|
public static String executeLinuxCmd(String cmd) {
|
try {
|
//System.out.println("got cmd job : " + cmd);
|
Runtime run = Runtime.getRuntime();
|
Process process;
|
process = run.exec(cmd);
|
InputStream in = process.getInputStream();
|
BufferedReader bs = new BufferedReader(new InputStreamReader(in));
|
StringBuffer out = new StringBuffer();
|
byte[] b = new byte[8192];
|
for (int n; (n = in.read(b)) != -1;) {
|
out.append(new String(b, 0, n));
|
}
|
|
in.close();
|
process.destroy();
|
return out.toString();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
*
|
* @param cmd 命令语句
|
* @param record 要查看的字段
|
* @param symbol 分隔符
|
* @return
|
*/
|
public static String getSerialNumber(String cmd ,String record,String symbol) {
|
String execResult = executeLinuxCmd(cmd);
|
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];
|
}
|
}
|
|
return null;
|
}
|
|
}
|