Is there any way you can find the IP of the server that the browser is connected to? For e.g. if the browser is accessing http://www.google.com, can we tell in any way what IP it is connected to? This is very useful in cases where Round-robin DNS is implemented. So say, the first request to a.com
results in 1.1.1.1
& subsequent request result in 1.1.1.2
& so on.
I couldn't find any way to do it with JavaScript. Is it even possible? If not is there any universal way to find this info out?
Actually there is no way to do this through JavaScript, unless you use some external source. Even then, it might not be 100% correct.
I believe John's answer is correct. For instance, I'm using my laptop through a wifi service run by a conference centre -- I'm pretty sure that there is no way for javascript running within my browser to discover the IP address being used by the service provider. On the other hand, it may be possible to address a suitable external resource from javascript. You can write your own if your own by making an ajax call to a server which can take the IP address from the HTTP headers and return it, or try googling "find my ip". The cleanest solution is probably to capture the information before the page is served and insert it in the html returned to the user. See How to get a viewer's IP address with python? for info on how to capture the information if you are serving the page with python.
Try this as a shortcut, not as a definitive solution (see comments):
This solution cannot work in some scenarios but it can help for quick testing. Regards
Can do this thru a plug-in like Java applet or Flash, then have the Javascript call a function in the applet or vice versa (OR have the JS call a function in Flash or other plugin ) and return the IP. This might not be the IP used by the browser for getting the page contents. Also if there are images, css, js -> browser could have made multiple connections. I think most browsers only use the first IP they get from the DNS call (that connected successfully, not sure what happens if one node goes down after few resources are got and still to get others - timers/ ajax that add html that refer to other resources).
If java applet would have to be signed, make a connection to the window.location (got from javascript, in case applet is generic and can be used on any page on any server) else just back to home server and use java.net.Address to get IP.
Not sure how to get the IP address specifically, but the location object provides part of the answer.
e.g. these variables might be helpful:
self.location.host
- Sets or retrieves the hostname and port number of the locationself.location.hostname
- Sets or retrieves the host name part of the location or URL.I think you may use the callback from a JSONP request or maybe just the pure JSON data using an external service but based on the output of javascript
location.host
that way:I'll use this code for my personal needs, as first I was coming on this site for the same reason.
You may use another external service instead the one I'm using for my needs. A very nice list exist and contains tests done here https://stackoverflow.com/a/35123097/5778582