简体   繁体   中英

get the common mac addresses on mac os using java

I'm building a java application that gets the mac addresses of a user and compare it with the correspondent value in the database(security feature). but the problem happens on mac os when i discovered that the list of mac addresses has common values(ex: on my mac the list of mac addresses are: 001C42000009,001C42000008,E0F8474267B6(wifi),70CD60F1A5C1(ethernet)) Is there a way to know all these common values that will result when getting the Mac address on Mac os.

Thank you.

i believe something like this will do the work for you

try {
    InetAddress []addresses = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());
    /*
     * Get NetworkInterfaces for current host and read hardware addresses.
     */
    for(int j=0; j< addresses.length; i++) {
            System.out.format("%02X%s", mac[i], (i < addresses.length – 1) ? "-" : "");
        }
        System.out.println();
    }
}

At http://standards.ieee.org/develop/regauth/oui/public.html you can lookup a vendor using first 3 bytes of the MAC address, 00-1C-42 points to "Parallels, Inc." (http://www.parallels.com). Are you using some of their virtualization software? Try what java.net.NetworkInterface.isVirtual() returns for this address, if that is not useful then some ugly filter may require (eg based on address pattern)

import java.net.NetworkInterface;
import java.util.Enumeration;

public class NetworkInterfaceTest {

  public static void main(String args[]) {
    try {
      Enumeration<NetworkInterface> ie = NetworkInterface.getNetworkInterfaces();
      while (ie.hasMoreElements()) {
        NetworkInterface i = ie.nextElement();
        System.out.println(i.getDisplayName() + " [" + i.getName() + "]: " + formatAddress(i.getHardwareAddress()) + "; isVirtual=" + i.isVirtual());
      }
    } catch (Exception e){ 
      e.printStackTrace();
    }
  }

  private static String formatAddress(byte[] address) {
    if (address == null) {
      return null;
    }

    StringBuilder ret = new StringBuilder(address.length * 2);
    for (byte b : address) {
      if (ret.length() > 0) {
        ret.append('-');
      }

      String bs = Integer.toHexString(b & 0x000000FF).toUpperCase();
      if (bs.length() < 2) {
        ret.append('0');
      }
      ret.append(bs);
    }

    return ret.toString();
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM