Lolly
Lolly

Reputation: 36452

Java find whether two server are running in same machine

I have to find whether two servers are running in same host.

I have two hosts but I can't compare them since one can have an ip address and the other may have a hostname. When I dug further I found there can be any number of aliases for a hostname. So how can I find out if two host aliases (where one can be an IP) correspond to same host or different host?

We can find the hostname using java.net.InetAddress.getLocalHost().getHostName(); but I am not sure how to find if two hostnames are pointing to same host?

Upvotes: 1

Views: 645

Answers (3)

Mikkel Løkke
Mikkel Løkke

Reputation: 3749

Do an NSlookup of the two host names. Compare results.

You can use InetAddress.getHostAddress() to perform the lookup.

Note that the host names may be behind a load balancer or other type of proxy, so there is no reliable way to test if they're actually "running on the same (physical) machine".

Upvotes: 0

R Kaja Mohideen
R Kaja Mohideen

Reputation: 917

  • Get the IP Address(es) for the Hostname. [Use: InetAddress::getByName()]
  • Get the Network Interface(s) for the IP(s) you have got. [Use: NetworkInterface::getByInetAddress()]
  • Get the MAC Address(es) for the Network Interface(s) [Use: NetworkInterface::getHardwareAddress()]
  • If there is an overlap of MAC Address generated by both Servers, they're running in same machine.

Upvotes: 0

Aiden Thompson
Aiden Thompson

Reputation: 326

You could convert everything to an IP Address and, since we know these are unique, compare those together.

Have a look at this article.

http://www.java2s.com/Code/Java/Network-Protocol/ConvertahostnametotheequivalentIPaddress.htm

Upvotes: 1

Related Questions