Solved

Unique computer ID using MAC.

Posted on 2008-10-25
5
3,231 Views
Last Modified: 2013-12-22
I need to uniquely identify a computer, and MAC address is the best option i could find.
I am very new to Java (in fact this is my first project outside of the sample codes from the textbook) and cannot think of a good solution to this.

I came up with this code that gets the MAC addresses for the NICs, and it does the job for the most part except on Windows, especially Vista, where it returns 3 addresses out of somewhere when the computer has only 1 NIC.   On linux it works just fine (no surprises there).
Vista gives me:
00 06 4f 5c e8 8f
02 00 54 55 4e 01
00 00 00 00 00 00 00 e0

where only the first one is the real interface.  Also, if i have 2 NICs, then there is another IPv6 address pops up making a total of 5 addresses.

So how can i modify this code so that i get only real hardware addresses, not something made up by the OS.
The emphasis goes on "real hardware" address because i have XP with VMware on it and VMware adapters also show up (and i don't think they are randomly generated).  I doubt this is possible though, but cannot think of a workaround.

Thank you.
import java.io.*;
import java.net.*;
import java.util.*;
 
public class MacAddress {
   public static void main (String args[]) throws SocketException
   {
      byte[] macAddress;
      Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
      for (NetworkInterface netint : Collections.list(nets)){
         macAddress = netint.getHardwareAddress();
         StringBuilder mac = new StringBuilder();
         if (macAddress != null){
            for (byte b : macAddress){
               mac.append(String.format("%1$02X ", b));
            }
         }
      System.out.println(mac);
      }
   }
}

Open in new window

0
Comment
Question by:AlexanderR
  • 3
  • 2
5 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22806014
You can use:

netint.getDisplayName()

This will give you the name of the interface that the MAC address is being generated for and then you can weed out the ones that you want.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22806028
You can also use netint.isUp() to limit the list to only the active connections.  A couple of the tunnel interfaces still show up, but can see what they are using the display name.
0
 
LVL 11

Author Comment

by:AlexanderR
ID: 22807069
>>weed out the ones that you want.

Not really an option.  This is a Java Applet that verifies uniqueness of user identity on a survey, so its going to go all over the place and i cannot take a chance in guessing all the names of interfaces out there that i may want to include or not.

But this brings me to another idea which is somewhat based on your suggestion.  If i get a list of all virtual OUIs, then i can just compare the first 6 digits against that list.  The applet communicates with a PHP page.  That page can do the job of filtering out "bad" MACs.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22807495
Good idea and good luck!
0
 
LVL 11

Author Closing Comment

by:AlexanderR
ID: 31510001
Thanks, I can apply this principle to MAC addresses instead, once i collect a comprehensive list.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question