5

I have a standalone command-line java app that running on server X. And it's required for me to know the unique ID of the machine where it running. How to get this ID? Maybe something like a hash. I don`t want to keep there something like a file with ID inside. Is there a way to get this unique ID that ill not depend on IP, hardware, etc?

Diversity
  • 1,890
  • 13
  • 20
avalon
  • 2,231
  • 3
  • 24
  • 49

1 Answers1

2

You can read out the MAC address of the server and use it as an unique key.

The following code snippet take from http://www.tutego.de/blog/javainsel/2013/12/mac-adressen-auslesen/ shows an possible implementation.

 for ( NetworkInterface ni : Collections.list(NetworkInterface.getNetworkInterfaces() ) ) {
   byte[] adr = ni.getHardwareAddress();
   if ( adr == null || adr.length != 6 )
      continue;
   String mac = String.format( "%02X:%02X:%02X:%02X:%02X:%02X",
                                adr[0], adr[1], adr[2], adr[3], adr[4], adr[5] );
   System.out.println( mac );
}

I am sorry that the source is in german but i am pretty sure that there exists an english documentation, too.

EDIT due to comment:

It must of course to be considered that also the MAC address can have duplicates.

The following link shows possible reasons https://serverfault.com/questions/462178/duplicate-mac-address-on-the-same-lan-possible

Either way using a MAC address as a solution for this problem is a pragmatic way.

Using hashing-methods: http://preshing.com/20110504/hash-collision-probabilities/

or

GUIDs: Is it safe to assume a GUID will always be unique?

also don't guarantee a 0.0% risk for possible duplicates.

Community
  • 1
  • 1
Diversity
  • 1,890
  • 13
  • 20
  • MAC addresses can be changed by the user and are only guaranteed to be unique within a subnet. – user207421 Apr 22 '15 at 21:01
  • @EJP i knew that this issue would be mentioned and for sure it can be a problem. But the probability of a clash is quite small. – Diversity Apr 22 '15 at 21:03
  • If you are in a virtualized world, say VMWare, moving the vm to a new host change the mac address, as well as other changes if certain configuration settings are right. – Steve Tarver Jun 19 '16 at 03:53