0

I am actually trying to print the mac addresses of the machine on which my web app is deployed. I have seen many questions related to mac addresses on SO, but I couldn't solve my issue. I have tried so far with the following code:

public static void main(String[] args) {
    getMac();
}

public static void getMac() {
    List<String> list = new ArrayList<String>();
    try {
        Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
        while (networks.hasMoreElements()) {
            NetworkInterface network = networks.nextElement();
            byte[] mac = network.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) && !list.contains(sb.toString())) {
                    if (!sb.toString().equals("00-00-00-00-00-00-00-E0")) {
                        list.add(sb.toString());
                    }
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    System.out.println("Mac Adresses List = " + list);
}

The code actually lists all the network interface mac addresses including virtual adapters. However I want to know if there is a way to find out the mac address of only those devices which exist on the machine. I have seen this post but it is not related to java. I want this information to uniquely identify a computer(though there are limitations with this approach).

Community
  • 1
  • 1
phoenix
  • 985
  • 3
  • 17
  • 38

1 Answers1

0

What about network.isVirtual() on NetworkInterface

epoch
  • 16,396
  • 4
  • 43
  • 71