Solved

Unique computer ID using MAC.

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 “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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now