I am a little confused regarding the return values from NetworkInterface.getNetworkInterfaces(). I am trying to filter through the list of NetworkInterface objects to figure out which are “real” (backed by actual hardware). The code looks like…
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
while ( e.hasMoreElements() ) {
NetworkInterface iface = (NetworkInterface) e.nextElement();
if ( !iface.isLoopback() && !iface.isVirtual() ) {
byte[] macAddress = iface.getHardwareAddress();
String message = String.format(
"%s %s parent: %s",
iface.getName(),
byteArrayToHexString( macAddress ),
iface.getParent() );
System.out.println( message );
}
}
…and the output like…
vnic1 00 1C 42 00 00 09 parent: null
vnic0 00 1C 42 00 00 08 parent: null
en1 60 33 4B 20 C4 97 parent: null
I know that the “vnic1” and “vnic0” ones aren’t “real” (they got put there by Parallels to support a VM I sometimes run on this machine). Is there a way through NetworkInterface that I can detect the not “real” ones or are they effectively indistinguishable from their actual counterparts, like “en1”? I would hate to have to resort to becoming dependent upon name like “its real if its name is en0 or en1”. Any ideas?
You could look up the first half of the MAC, as it is the vendor code. The majority of vendors probably make physical or virtual (not both) devices.
You won’t hit 100% of cases with this approach, but it might be “good enough” for your use.
It also won’t help you if someone spoofs the MAC address.
What are you trying to do here? If you explain your ultimate goal, maybe there is another approach.