0
votes

Hi im trying to print the Ethernet mac address of a computer but im unsure how to change my code from printing the wireless lan address i couldn't find any information on 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();
    }
}

im looking to print the Ethernet adapter Local Area Connection: instead of the Wireless LAN adapter Wireless Network Connection 3:

2
do both the connections have different ip? - piyushj
if i understand the code correctly retro it would change for every system i ran the program on depending on how many connections are available so it would end up printing the wrong address. correct me if im wrong tho - pvtctrlalt
the physical address of the wireless and the Ethernet are different yes. and because the wireless can change more easily if the wireless card is changed it does not work in my case. im looking to find something more permanent which the Ethernet physical address works well for - pvtctrlalt
Look at the class "NetworkInterface", with the infos returned there you should be able to determine the correct interface - reto

2 Answers

0
votes

You can try with this that prints all interfaces:

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();
    }
}

On my OSX box it prints:

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
0
votes

You can get all network interfaces and loop over them. If you are just looking for physical network adapters, you can filter them based on whether they have a MAC address.

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());
}

Which prints for me:

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