tags:

views:

407

answers:

5

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 DNS round robin 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?

A: 

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 location or self.location.hostname Sets or retrieves the host name part of the location or URL.

Matt H
+2  A: 

Fairly certain this cannot be done. However you could use your preferred server-side language to print the server's IP to the client, and then use it however you like. For example, in PHP:

<script type="text/javascript">
    var ip = "<?php echo $_SERVER['SERVER_ADDR']; ?>";
    alert(ip);
</script>

This depends on your server's security setup though - some may block this.

Jason Berry
We don't use PHP, but I get the idea. Regardless will this work even if the server is NATed?
John
I'm honestly not sure, you can give it a try - it's pretty simple. I created the test case that I posted in less than a minute. .NET, ColdFusion and I'm sure most other server-side languages have their equivalents!
Jason Berry
+1  A: 

You cannot get this in general. From Javascript, you can only get the HTTP header, which may or may not have an IP address (typically only has the host name). Part of the browser's program is to abstract the TCP/IP address away and only allow you to deal with a host name.

A: 

Actually there is no way to do this through JavaScript, unless you use some external source. Even then, it might not be 100% correct.

John
A: 

What do you want to use this information for? If the servers are your own and what you really want to know is 'which server generated this response?' a custom HTTP header whose value is dependent on the physical machine that generated the response might be your best bet - and then it doesn't have to be just an IP. If you want to map someone else's network, you should look for network mapping tools and understand that not many systems administrators take kindly to that kind of effort.

DDaviesBrackett