190679152@qq.com的gravatar头像
190679152@qq.com 2017-01-12 09:49:48

Linux系统java如何获取硬件唯一信息,比如MAC地址和CPU?

求Linux系统获取硬件唯一信息,比如MAC地址和CPU,通过这些信息实现客户机是否授权功能

所有回答列表(3)
190679152@qq.com的gravatar头像
190679152@qq.com  LV15 2017年1月12日

这个方法不行

/** * 获取主板序列号 * * @return */ public static String getMotherboardSN() { String result = ""; try { File file = File.createTempFile("realhowto", ".vbs"); file.deleteOnExit(); FileWriter fw = new java.io.FileWriter(file); String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n" + "Set colItems = objWMIService.ExecQuery _ \n" + " (\"Select * from Win32_BaseBoard\") \n" + "For Each objItem in colItems \n" + " Wscript.Echo objItem.SerialNumber \n" + " exit for ' do the first cpu only! \n" + "Next \n"; fw.write(vbs); fw.close(); Process p = Runtime.getRuntime().exec( "cscript //NoLogo " + file.getPath()); BufferedReader input = new BufferedReader(new InputStreamReader( p.getInputStream())); String line; while ((line = input.readLine()) != null) { result += line; } input.close(); } catch (Exception e) { e.printStackTrace(); } return result.trim(); } /** * 获取MAC地址 */ @SuppressWarnings("unused") public static String getLocalMac() { // TODO Auto-generated method stub StringBuffer sb = new StringBuffer(""); try{ //获取网卡,获取地址 InetAddress ia = InetAddress.getLocalHost(); byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress(); for(int i=0; i

码农_老王的gravatar头像
码农_老王  LV10 2017年1月12日

//==============================获取CPU序列号========

package com.test;

import java.io.IOException;
import java.util.Scanner;


public class CpuUtil {

 public static void main(String[] args) throws IOException {

  long start = System.currentTimeMillis();

  Process process = Runtime.getRuntime().exec(
    new String[] { "wmic", "cpu", "get", "ProcessorId" });

  process.getOutputStream().close();

  Scanner sc = new Scanner(process.getInputStream());

  String property = sc.next();

  String serial = sc.next();

  System.out.println(property + ": " + serial);

  System.out.println("time:" + (System.currentTimeMillis() - start));

 }
}

 


//=======================获取硬盘序列号==========================

 package com.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;

public class DiskUtil {
 public static String getSerialNumber(String drive) {
    String result = "";
      try {
        File file = File.createTempFile("realhowto",".vbs");
        file.deleteOnExit();
        FileWriter fw = new java.io.FileWriter(file);

        String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
                    +"Set colDrives = objFSO.Drives\n"
                    +"Set objDrive = colDrives.item(\"" + drive + "\")\n"
                    +"Wscript.Echo objDrive.SerialNumber";  // see note
        fw.write(vbs);
        fw.close();
        Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
        BufferedReader input =
          new BufferedReader
            (new InputStreamReader(p.getInputStream()));
        String line;
        while ((line = input.readLine()) != null) {
           result += line;
        }
        input.close();
      }
      catch(Exception e){
          e.printStackTrace();
      }
      return result.trim();
    }

 public static void main(String[] args) {
   String sn = DiskUtil.getSerialNumber("C");
   System.out.println(sn);
 }

 

 

//=============================获取主板序列号====================

package com.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;


public class MiscUtil {

 public static String getMotherboardSN() {
  String result = "";
  try {
   File file = File.createTempFile("realhowto", ".vbs");
   file.deleteOnExit();
   FileWriter fw = new java.io.FileWriter(file);

   String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
     + "Set colItems = objWMIService.ExecQuery _ \n"
     + "   (\"Select * from Win32_BaseBoard\") \n"
     + "For Each objItem in colItems \n"
     + "    Wscript.Echo objItem.SerialNumber \n"
     + "    exit for  ' do the first cpu only! \n" + "Next \n";

   fw.write(vbs);
   fw.close();
   Process p = Runtime.getRuntime().exec(
     "cscript //NoLogo " + file.getPath());
   BufferedReader input = new BufferedReader(new InputStreamReader(p
     .getInputStream()));
   String line;
   while ((line = input.readLine()) != null) {
    result += line;
   }
   input.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
  return result.trim();
 }

 public static void main(String[] args) {
  String cpuId = MiscUtil.getMotherboardSN();
  System.out.println(cpuId);
  
 }

}

wentao的gravatar头像
wentao  LV24 2017年1月13日

不介意的话,不妨试试 sigar !!!

顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友