Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java get unique server ID for reference

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?

like image 319
avalon Avatar asked Nov 01 '22 06:11

avalon


1 Answers

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.

like image 113
Diversity Avatar answered Nov 15 '22 04:11

Diversity