• java生成系统机器码


    package com.gdcy.qldlgf;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.Scanner;
    
    /**
     * 生成机器码
     */
    public class GeneratingMachineCode {
        /**
         * 生成序列号的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 = GeneratingMachineCode.getCPUSerialNumber();
                // 获取 硬盘号
                hardDiskSerialNumber = GeneratingMachineCode.getHardDiskSerialNumber();
            } else if (property.contains("linux")) {
                // 获取cpu序列号
                cpuSerialNumber = GeneratingMachineCode.getUUID();
                // 获取 硬盘号
                hardDiskSerialNumber = GeneratingMachineCode.getBoisVersion();
            }
            // 获取到cpu序列号和硬盘号
            System.out.println("key:" + cpuSerialNumber + hardDiskSerialNumber);
        }
    
        /**
         * 获取CPU序列号(Windows)
         *
         * @return
         * @throws
         */
        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
         * @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;
        }
    }
  • 相关阅读:
    oracle RAC 更换IP
    12C oracle 12.1.0.2版本打补丁
    node name配置错误,导致grid日志在报警
    input_subsys 输入子系统框架分析
    www.bing.com
    getopt函数使用说明
    FreeType 矢量字体 测试移植(1)
    字符的编码方式
    在开发板上显示字符和中文
    块设备驱动程序的框架
  • 原文地址:https://www.cnblogs.com/tszr/p/16384555.html
Copyright © 2020-2023  润新知