Java:获取本机的所有Mac地址

 mobiledu2502872407 发布于 2022-10-25 09:30
public class Mac {
    public static void main(String[] args) {
        try {
            Enumeration enumeration = NetworkInterface.getNetworkInterfaces();
            while (enumeration.hasMoreElements()) {
                StringBuffer stringBuffer = new StringBuffer();
                NetworkInterface networkInterface = enumeration.nextElement();
                if (networkInterface != null) {
                    byte[] bytes = networkInterface.getHardwareAddress();
                    if (bytes != null) {
                        for (int i = 0; i < bytes.length; i++) {
                            if (i != 0) {
                                stringBuffer.append("-");
                            }
                            int tmp = bytes[i] & 0xff; // 字节转换为整数
                            String str = Integer.toHexString(tmp);
                            if (str.length() == 1) {
                                stringBuffer.append("0" + str);
                            } else {
                                stringBuffer.append(str);
                            }
                        }
                        String mac = stringBuffer.toString().toUpperCase();  
                        System.out.println(mac);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

在我的开发机(Windows)上运行上面代码可以获取多个Mac地址(安装了VM有若干虚拟的),但在服务器上却只能获取到一个Mac地址,请问这是为什么呢?


我却只能获取到【XX-XX-54-B3-6B-28】这一个Mac请问为什么呢?应该一个是有线网卡,一个是无线网卡获取到两个才对,可是【XX-XX-54-B3-6B-29】却获取不到

===========================分割线============================

仍未找到问题所在,临时解决办法是Windows中使用以上代码获取所有Mac,而在Linux(CentOS 7.0)中通过读取

public class Mac {
    public static void main(String[] args) throws Exception {
        StringBuffer sb = new StringBuffer();
        try {
            String str = "cat /sys/class/net/*/address | sed -n '1p'";
            String[] cmd = new String[] { "/bin/sh", "-c", str };
            Process process = Runtime.getRuntime().exec(cmd);
            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        String mac = sb.toString().replace(":", "-").toUpperCase();
    }
}
3 个回答
  • 你服务器上还装了无线网卡? 我台式机都没装

    2022-10-26 22:57 回答
  • InetAddress address = InetAddress.getLocalHost(); 
            NetworkInterface ni = NetworkInterface.getByInetAddress(address); 
            byte[] macs = ni.getHardwareAddress(); 
            String mac = ""; 
            Formatter formatter = new Formatter(); 
            for (int i = 0; i < macs.length; i++) { 
                mac = formatter.format(Locale.getDefault(), "%02X%s", macs[i], i < macs.length - 1 ? "-" : "").toString(); 
            } 
            return mac; 
    2022-10-26 22:58 回答
  • 你服务器上 eno2 那个网络接口的 flag 没有 running 状态,是不是没启用?
    用 mii-tool 之类的命令检查一下。

    2022-10-26 22:58 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有