1

I have a requirement in which I need to display both IP address and hostname of the client machine. I have to use the chrome browser only for this activity. Below are the ways I tried.

Approach 1 - Using HttpServletRequest

HttpServletRequest request = RequestFilter.getRequest();
String ClientIP = request.getRemoteAddr();
String hostName = request.getRemoteHost();
logger.info("IP of the client:" + ClientIP);
logger.info("Host Name of the client :" + hostName);

With this approach request.getRemoteHost() returns me the public ip as the hostname and not the Coumpter's name.

Approach 2 - Using Inet Address

InetAddress IP = InetAddress.getLocalHost();
String ClientIP = IP.getHostAddress();
String hostName = InetAddress.getLocalHost().getHostName();
logger.info("IP of client:"+ClientIP);
logger.info("Host Name of client:"+hostName);

This approach give me server's IP and hostname details. Not Clients.

I tried to get hostname by IP also using

InetAddress.getByName(request.getRemoteAddr()).getHostName()

But, this one too returns client's public IP and not the Hostname.

I tried using Javascript to get the hostname, but since I have to use only Chrome browser, I am unable to use ActiveXObject.

On running hostname command on cmd, I'm able to obtain the correct computer name which is my desired result.

Please suggest some way in which I can get the computer name as the hostname of the client. Please help, I'm stuck badly.

Harshita Sethi
  • 2,035
  • 3
  • 24
  • 46
  • As far as I know, you can't. – Robby Cornelissen Jan 11 '18 at 08:04
  • @RobbyCornelissen Is there any explanation for this? When IP is obtainable then why not hostname? I need to provide any explanation to my clients for the same. So can you help with that.? – Harshita Sethi Jan 11 '18 at 08:11
  • Are the clients in the same network/organization as the server or is it a more or less public system? If the latter you'll very likely not be able to access that information, if the former you might be able to do some "inverse" DNS lookup, i.e. get the hostname by ip address. – Thomas Jan 11 '18 at 08:11
  • "When IP is obtainable then why not hostname?" - The ip address is what's actually used for the connectio, host/domain names are mostly a human readable name used to look up the ip address. – Thomas Jan 11 '18 at 08:12
  • You're asking to retrieve the computer's name. Is that name correlated with the IP address you're getting? – Robby Cornelissen Jan 11 '18 at 08:16
  • @Thomas Both the clients and server are in same network. I tried using IP address to get hostname using InetAddress as mentioned in the question, but the result is again an IP. I tried `InetAddress.getByName(request.getRemoteAddr()).getHostName()`. – Harshita Sethi Jan 11 '18 at 08:17
  • @RobbyCornelissen It's not correlated with IP. Its a fixed name assigned to each client workstation in my organisation. – Harshita Sethi Jan 11 '18 at 08:20
  • The problem is that if the client doesn't send that hostname (you could have a look at which http headers are set in the request) then your server can't just magically extract that name somehow. Thus the only other way I could think of would be to use a correlation between ip and hostname, i.e. ask some service ("inverse DNS") for the hostname associated to that ip. If that correlation doesn't exist I'm afraid you can't get the hostname – Thomas Jan 11 '18 at 08:25
  • I doubt that the clients will send their hostname via http headers though since they'd do that in requests to public sites as well, which could be a security problem (the hostname could provide information to hackers), would be unnecessary and could not fit the ip anyways (e.g. if there's some NAT involved). – Thomas Jan 11 '18 at 08:27
  • @Thomas Can I set some http headers, to get the hostname? – Harshita Sethi Jan 11 '18 at 09:27
  • yes you will get the computer name – spandey Jan 11 '18 at 09:53

0 Answers0