| | |
| | | import javax.annotation.Resource; |
| | | import java.applet.Applet; |
| | | import java.awt.Graphics; |
| | | import java.io.BufferedReader; |
| | | import java.io.File; |
| | | import java.io.FileWriter; |
| | | import java.io.InputStreamReader; |
| | | import java.io.*; |
| | | import java.net.InetAddress; |
| | | import java.net.NetworkInterface; |
| | | import java.util.Scanner; |
| | | |
| | | public class HardWareUtils extends Applet { |
| | | public HardWareUtils(){ |
| | |
| | | |
| | | } |
| | | |
| | | /**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; |
| | | } |
| | | |
| | | /** |
| | | * CPU序列号-系统兼容 |
| | | * @return |
| | | */ |
| | | public static String getCPUNumber(){ |
| | | 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", "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 |
| | | serial =getSerialNumber("dmidecode -t processor | grep 'ID'", "ID",":"); |
| | | } |
| | | |
| | | return serial; |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 主板序列号-系统兼容 |
| | | */ |
| | | public static String getBaseboardNumber(){ |
| | | 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", "baseboard", "get", "serialnumber"}); |
| | | 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 void main(String[] args) throws Exception { |
| | | System.out.println(getCPUSerial());//CPU |
| | | System.out.println(getMotherboardSN());//主板 |
| | | System.out.println(getHardDiskSN("c"));//c盘 |
| | | System.out.println(getMac());//MAC |
| | | String msg = getCPUSerial()+getMotherboardSN().replace(".", "")+getHardDiskSN("c")+getMac().replace("-", ""); |
| | | System.out.println("原始数据:"+msg); |
| | | System.out.println(getBaseboardNumber()); |
| | | |
| | | |
| | | } |