java打印以太网mac地址

时间:2016-05-06 09:32:04

标签: java mac-address

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

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:

2 个答案:

答案 0 :(得分:0)

您可以获取所有网络接口并循环它们。如果您只是在寻找物理网络适配器,可以根据它们是否具有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

答案 1 :(得分:0)

您可以尝试打印所有界面:

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