Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Unique computer ID using MAC.

Posted on 2008-10-25
5
Medium Priority
?
3,248 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 60

Accepted Solution

by:
Kevin Cross earned 1500 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 60

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 60

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

773 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