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

2 篇文章 0 订阅
1 篇文章 0 订阅
/**
 * 生成序列号的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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值