Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need to get the TRUE client IP address

 
Christopher Swearingen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been searching the web and CodeRanch forum boards all morning.

I need to get the IP address for the browser's machine and not the host's IP. The reason is that I've got several kiosk machines on the WAN that display a specific intranet page and I need to monitor them. The kiosks all get their IP via DHCP. I want to have a jQuery AJAX call that will pass the IP address and session ID so I can record it in a database. So I either need to get the IP address client-side via Javascript or use the HTTP Request server-side. I need to retrieve the browser's private network IP (e.g. 192.168.1.1, 10.167.1.37, etc.) but the problem is I don't think I can get the local IP via the request?

To test the process I've got a tomcat server running locally on my dev machine and I tried testing request.getLocalAddr(), request.getHeader("x-forwarded-for"), etc. Everything I try returns server info rather than client (127.0.0.1/localhost/null/etc.).

Anyone have any resources they can point me to? Using a third part site like http://jsonip.appspot.com/?callback=getip won't work as that gives the external IP Address.
 
Christopher Swearingen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For any interested I was able to accomplish this. Since all the computers I need to monitor are on the WAN (and behind the same firewall) I created a class that implements ServletRequestAware. That class uses the Request object to get the IP address via .getRemoteAddr(). Then the IP address is fed through java.net.InetAddress.getByName(ip_address) to get the hostname. Worked for me!
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to hear you found a solution for your case. You do realize, of course, that this is not a general solution. These days its rare for a client to have a real IP address, they are usually behind at least one level of NAT routing, so you can't get the real address.
 
Christopher Swearingen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:Glad to hear you found a solution for your case. You do realize, of course, that this is not a general solution. These days its rare for a client to have a real IP address, they are usually behind at least one level of NAT routing, so you can't get the real address.


Yeah this was one very specific instance where I needed to get this working. But I figure if I came across the need then someone else would too. ;)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic