【问题标题】:java printing ethernet mac addressjava打印以太网mac地址
【发布时间】:2016-09-01 07:45:20
【问题描述】:

您好,我正在尝试打印计算机的以太网 MAC 地址,但我不确定如何更改我的代码以打印无线局域网地址,我在 google 上找不到任何信息。

public void getMacAddress(){
    InetAddress ip;
    try {
        ip = InetAddress.getLocalHost();
        NetworkInterface network = NetworkInterface.getByInetAddress(ip);
        byte[] mac = network.getHardwareAddress();
        System.out.print("Current MAC address : ");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mac.length; i++) {
            sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));        
        }
        System.out.println(sb.toString());
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (SocketException e){
        e.printStackTrace();
    }
}

我正在寻找打印以太网适配器本地连接:而不是 无线 LAN 适配器 无线网络连接 3:

【问题讨论】:

  • 这个答案有帮助吗? stackoverflow.com/questions/20350150/…
  • 两个连接有不同的ip吗?
  • 如果我正确理解了代码,它会根据我运行程序的每个系统发生变化,具体取决于可用的连接数,因此最终会打印错误的地址。如果我错了,请纠正我
  • 无线和以太网的物理地址不同是的。并且因为如果更换无线卡,无线可以更轻松地更改,因此在我的情况下它不起作用。我正在寻找更永久的东西,以太网物理地址适用于
  • 查看“NetworkInterface”类,根据那里返回的信息,您应该能够确定正确的接口

标签: java mac-address


【解决方案1】:

您可以尝试打印所有接口:

public static void printInterfaces() {
    Enumeration<NetworkInterface> networkInterfaces;
    try {
        networkInterfaces = NetworkInterface.getNetworkInterfaces();
        Collections.list(networkInterfaces).forEach(networkInterface -> {

            try {
                StringBuilder sb = new StringBuilder();
                if (networkInterface.getHardwareAddress() != null) {
                    byte[] mac = networkInterface.getHardwareAddress();
                    for (int i = 0; i < mac.length; i++) {
                        sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
                    }
                } else {
                    sb.append("Interface has no MAC");
                }
                System.out.println(
                        String.format("Interface: %s  MAC: %s", networkInterface.getDisplayName(), sb.toString()));
            } catch (SocketException e) {
                e.printStackTrace();
            }
        });
    } catch (SocketException e1) {
        e1.printStackTrace();
    }
}

在我的 OSX 盒子上打印:

Interface: awdl0  MAC: 16-D0-14-AA-AA-AA
Interface: vboxnet0  MAC: 0A-00-27-00-00-00
Interface: en1  MAC: A8-86-DD-AA-AA-AA
Interface: en0  MAC: 68-5B-35-AA-AA-AA
Interface: lo0  MAC: Interface has no MAC

【讨论】:

  • 我真的只需要以太网mac地址,因为不同的系统有不同的连接数量,如果我只是用这种方法挑选一个,每台电脑都会改变
  • 有没有办法使用这种方法只打印以太网?
  • 通过编辑显示部分,我设法让它只显示带有 macadresses 的地址 if(!sb.toString().isEmpty()) System.out.println(String.format(sb. toString()));
  • 据我了解,您需要以太网连接的 Mac。 “以太网连接”是具有物理 RJ45 连接的连接。我的理解正确吗?
  • 当前显示的是 94-DE-80-50-4B-A5 00-00-00-00-00-00-00-E0 C8-3A-35-C8-D8-F6 00-00-00-00-00-00-00-E0 我只需要顶部地址
【解决方案2】:

您可以获取所有网络接口并循环访问它们。如果您只是在寻找物理网络适配器,您可以根据它们是否具有 MAC 地址来过滤它们。

public static void main(String[] args) throws SocketException {
    Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
    while (networkInterfaces.hasMoreElements()) {
        NetworkInterface networkInterface = networkInterfaces.nextElement();
        System.out.printf("%s with %s%n", networkInterface.getDisplayName(),
                macString(networkInterface).orElse("no hardware address"));
    }
}

/**
 * Gets the hardware address of a {@link NetworkInterface} in the format
 * {@code AA-BB-CC-DD-EE-FF}.
 *
 * @param iface The interface to get the MAC address string of.
 * @return A optional containing the string representation. This optional will
 * be empty if the network interface does not have a hardware address (virtual adapters like
 * loopback devices).
 * @throws SocketException If an I/O error occurs when getting the hardware address from the interface.
 */
private static Optional<String> macString(NetworkInterface iface) throws SocketException {
    byte[] mac = iface.getHardwareAddress();
    if (mac == null) {
        return Optional.empty();
    }
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < mac.length; i++) {
        sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
    }
    return Optional.of(sb.toString());
}

为我打印:

tun0 with no hardware address
eth0 with AE-50-74-CC-73-ED
lo with no hardware address

【讨论】:

  • 在尝试了它接缝的代码以打印所有将出现在 ipconfig/all 中的内容
猜你喜欢
  • 2016-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 2011-11-12
相关资源
最近更新 更多