Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Unique computer ID using MAC.

Posted on 2008-10-25
5
Medium Priority
?
3,244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

722 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