java生成系统机器码(linux和windows)

/**
 * 生成序列号的main方法
 * @param args
 */
public static void main(String[] args) {
    String property = System.getProperty("os.name").toLowerCase();
    String cpuSerialNumber = "";
    String hardDiskSerialNumber = "";
    String md5Result = "";
    if (property.contains("windows")) {
        // 获取cpu序列号
        cpuSerialNumber = GenerateRegistCode.getCPUSerialNumber();
        // 获取 硬盘号
        hardDiskSerialNumber = GenerateRegistCode.getHardDiskSerialNumber();
    } else if (property.contains("linux")) {
        // 获取cpu序列号
        cpuSerialNumber = GenerateRegistCode.getUUID();
        // 获取 硬盘号
        hardDiskSerialNumber = GenerateRegistCode.getBoisVersion();
    }
    // 获取到cpu序列号和硬盘号  
       System.out.println("key:" + cpuSerialNumber + hardDiskSerialNumber);
}
/**
 * 获取CPU序列号(Windows)
 *
 * @return
 * @throws IOException
 */
public static String getCPUSerialNumber() {
    String serial;
    try {
        Process process = Runtime.getRuntime().exec(new String[]{"wmic", "cpu", "get", "ProcessorId"});
        process.getOutputStream().close();
        Scanner sc = new Scanner(process.getInputStream());
        serial = sc.next();
    } catch (IOException e) {
        throw new RuntimeException("获取CPU序列号失败");
    }
    return serial;
}
/**
 * 获取 硬盘序列号(Windows)
 *
 * @return
 * @throws IOException
 * @throws InterruptedException
 */
public static String getHardDiskSerialNumber() {
    String serial;
    try {
        Process process = Runtime.getRuntime().exec(new String[]{"wmic", "path", "win32_physicalmedia", "get", "serialnumber"});
        process.getOutputStream().close();
        Scanner sc = new Scanner(process.getInputStream());
        serial = sc.next();
    } catch (IOException e) {
        throw new RuntimeException("获取硬盘序列号失败");
    }
    return serial;
}
/**
* 注:liunx上 如果想获取的话  需要root用户来执行 ;如果使用普通用户 执行的话  需要输入当前用户的密码(普通用户不支持dmidecode命令 因为没权限)
*/

/**
 * bois版本号(linux)
 *
 * @return
 */
public static String getBoisVersion() {
    String result = "";
    Process p;
    try {
        p = Runtime.getRuntime().exec("sudo dmidecode -s bios-version");// 管道
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while ((line = br.readLine()) != null) {
            result += line;
            break;
        }
        br.close();
    } catch (IOException e) {
        System.out.println("获取主板信息错误");
    }
    return result;
}


/**
 * 获取系统序列号(linux)
 *
 * @return
 */
public static String getUUID() {
    String result = "";
    try {
        Process process = Runtime.getRuntime().exec("sudo dmidecode -s system-uuid");
        InputStream in;
        BufferedReader br;
        in = process.getInputStream();
        br = new BufferedReader(new InputStreamReader(in));
        while (in.read() != -1) {
            result = br.readLine();
        }
        br.close();
        in.close();
        process.destroy();
        // System.out.println("获取序列号:"+result);
    } catch (Throwable e) {
        e.printStackTrace();
    }
    return result;
}
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读