java获取操作系统的MAC地址和硬盘序列号

1.判断操作系统是Windows还是Linux

private static Boolean isLinux() {
    String os = System.getProperty("os.name");
    log.info("os.name: {}", os);
    return !os.toLowerCase().startsWith("win");
}

 

2. Linux:

  获取MAC地址

private static String getMACAddressByLinux() throws Exception {
        String[] cmd = {"ifconfig"};

        Process process = Runtime.getRuntime().exec(cmd);
        process.waitFor();

        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
        StringBuffer sb = new StringBuffer();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }

        String str1 = sb.toString();
        String str2 = str1.split("ether")[1].trim();
        String result = str2.split("txqueuelen")[0].trim();
        log.info("Linux MacAddress is: {}", result);
        br.close();

        return result;
    }

  获取硬盘序列号

private static String getIdentifierByLinux() throws Exception {
        String[] cmd = {"fdisk", "-l"};

        Process process = Runtime.getRuntime().exec(cmd);
        process.waitFor();

        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
        StringBuffer sb = new StringBuffer();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }

        String str1 = sb.toString();
        String str2 = str1.split("identifier:")[1].trim();
        String result = str2.split("Device Boot")[0].trim();
        log.info("Linux Identifier is: {}", result);
        br.close();

        return result;
    }

 

3. Windows:

  获取MAC地址: (默认获取第一张网卡)

private static String getMACAddressByWindows() throws Exception {
        String result = "";
        Process process = Runtime.getRuntime().exec("ipconfig /all");
        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));

        String line;
        int index = -1;
        while ((line = br.readLine()) != null) {
            index = line.toLowerCase().indexOf("物理地址");
            if (index >= 0) {// 找到了
                index = line.indexOf(":");
                if (index >= 0) {
                    result = line.substring(index + 1).trim();
                }
                break;
            }
        }
        log.info("Windows MACAddress is: {}", result);
        br.close();
        return result;
    }

  获取硬盘序列号: (默认获取C盘)

private static String getIdentifierByWindows() throws Exception {
        String result = "";
        Process process = Runtime.getRuntime().exec("cmd /c dir C:");
        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));

        String line;
        while ((line = br.readLine()) != null) {
            if (line.indexOf("卷的序列号是 ") != -1) {
                result = line.substring(line.indexOf("卷的序列号是 ") + "卷的序列号是 ".length(), line.length());
                break;
            }
        }
        log.info("Windows Identifier is: {}", result);
        br.close();
        return result;
    }

 

4. 测试:

public static void main(String[] a) throws Exception {
        // 判断是Linux还是Windows
        if (isLinux()) {
            // Linux操作系统
            String macAddress = getMACAddressByLinux();
            System.out.println("Linux macAddress: " + macAddress);
            String Identifier = getIdentifierByLinux();
            System.out.println("Linux Identifier: " + Identifier);
        } else {
            // Windows操作系统
            String macAddress = getMACAddressByWindows();
            System.out.println("Windows macAddress: " + macAddress);
            String Identifier = getIdentifierByWindows();
            System.out.println("Windows Identifier: " + Identifier);
        }
    }

 

注意事项:

  在Windows环境使用javac Test.java 命令编译该java文件时, 需指定编码, 应使用以下命令:

javac -encoding UTF-8 Test.java

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值