0

The following java code will convert an IP Address to its host.

InetAddress addr = InetAddress.getByName("31.13.78.13");
String host = addr.getHostName();
System.out.println(host);

Here host will be printed as "edge-star-shv-01-sit4.facebook.com". The first portion may be the address of a data center of facebook. I only want the "facebook.com" portion. I could not find any built in method in InetAddress library that can sort this out. I can solve the problem using substring method but this is not a good way because I want a generalized approach. Is there any way where I can get "facebook.com" instead of "edge-star-shv-01-sit4.facebook.com" by taking "31.13.78.13" as input?

  • check https://stackoverflow.com/questions/9607903/get-domain-name-from-given-url – Mzf Nov 26 '17 at 11:42

1 Answers1

1

You can't!

I run multiple websites on one IP adress. So if you would use a script as you discribe it here, what should it return?

The only thing is to get the hostname of the server with the selected IP, this can contain the website name , as in your case, but it doesn't have to. Imagine a server with hostname "example.com" and IP 1.1.1.1, or whatever, on this machine a website in running with url "stackoverflow.com" then you expect when tracing the IP adress to get "stackoverflow.com" but in fact you would get "example.com"

Tobias Schäfer
  • 1,330
  • 2
  • 17
  • 35