• 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;
        }
    }
  • 相关阅读:
    MessageFormat使用记录
    在IDEA中使用MyBatis Generator逆向工程生成代码
    mybatis报错invalid types () or values ()解决方法
    关于jrebel碰到的一次问题记录
    I/O限制异步操作
    线程基础和异步执行计算限制
    模板方法模式(Head first 设计模式——7)
    适配器模式和外观模式(head first设计模式——6)
    命令模式(head first 设计模式5)
    工厂模式——(Head first设计模式4)
  • 原文地址:https://www.cnblogs.com/tszr/p/16384555.html
Copyright © 2020-2023  润新知