JAVA获取机器的MAC地址

JAVA获取机器的MAC地址

需求:获取机器的mac地址

问题:一些国产操作系统获取不到MAC地址,以下搜集了一些获取mac地址的方法

1.获取MAC地址

ps:该方法在银河麒麟中可用

public static String getMAC() throws SocketException {
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();

        while(networkInterfaces.hasMoreElements()) {
            NetworkInterface network = networkInterfaces.nextElement();
            log.info("network:{}", network);
            byte[] mac = network.getHardwareAddress();
            if(mac == null) {
                log.warn("mac is null");
            } else {

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < mac.length; i++) {
                    sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
                }
                log.info("MAC address : {} ", sb);

                if (!"".equals(sb.toString())) {
                    return sb.toString();
                }
                break;
            }
        }
        return "";
    }

2.获取MAC地址

在UOS系统中可用

public static List<String> getMacAddress() {

        ArrayList<String> macs = new ArrayList<>();
        try {
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            byte[] mac = null;
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                if (netInterface.isLoopback() || netInterface.isVirtual() || netInterface.isPointToPoint() || !netInterface.isUp()) {
                    continue;
                } else {
                    mac = netInterface.getHardwareAddress();
                    if (mac != null) {
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < mac.length; i++) {
                            sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""));
                        }
                        if (sb.length() > 0) {
                            macs.add(sb.toString().toLowerCase());
                        }
                    }
                }
            }
            log.info("获取的mac地址为:{}", macs);
            return macs;
        } catch (Exception e) {
            log.error("MAC地址获取失败", e);
        }
        return Collections.emptyList();
    }

3.获取Linux的MAC地址

public static List<String> getLinuxMACAddress() {
        List<String> macs = new ArrayList<>();
        BufferedReader bufferedReader = null;
        Process process = null;
        try {
            process = Runtime.getRuntime().exec("ifconfig -a");
            bufferedReader = new BufferedReader(new InputStreamReader(
                    process.getInputStream()));
            String line = null;
            int index = -1;
            while ((line = bufferedReader.readLine()) != null) {
                index = line.toLowerCase().indexOf("硬件地址");
                String mac = "";
                if (index != -1) {
                    mac = line.substring(index + 4).trim();
                    macs.add(mac.toLowerCase());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            bufferedReader = null;
            process = null;
        }
        log.info("MAC address :{}", macs);
        return macs;
    }

4.获取windows的mac地址

public static List<String> getMACAddressByWindows() throws Exception {
        ArrayList<String> rs = new ArrayList<>();
        String result = "";
        Process process = Runtime.getRuntime().exec("ipconfig /all");
        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
        int index = -1;
        String line;
        while ((line = br.readLine()) != null) {
            index = line.indexOf("物理地址");
            if (index >= 0) {
                index = line.indexOf(":");
                if (index >= 0) {
                    result = line.substring(index + 1).trim();
                }
                rs.add(result.toUpperCase());
            }
        }
        br.close();
        log.info("Mac address : {}",rs);
        return rs;
    }
  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值