Solved

Unique computer ID using MAC.

Posted on 2008-10-25
5
3,240 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 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.
Suggested Courses

617 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