32

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?

informatik01
  • 15,174
  • 9
  • 67
  • 100
John
  • 1,511
  • 3
  • 15
  • 27
  • A way could be making a DNS lookup for `location.host` through ajax. That wouldn't be entirely JS though. – rdiz Apr 14 '15 at 07:28
  • See this as related question: http://stackoverflow.com/questions/102605/can-i-perform-a-dns-lookup-hostname-to-ip-address-using-client-side-javascript – D Adams Dec 05 '15 at 06:34

10 Answers10

51

Try this as a shortcut, not as a definitive solution (see comments):

<script type="text/javascript">
    var ip = location.host;
    alert(ip);
</script>

This solution cannot work in some scenarios but it can help for quick testing. Regards

Spacemonkey
  • 1,593
  • 3
  • 18
  • 43
16

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
  • 2,439
  • 1
  • 16
  • 21
  • 1
    We don't use PHP, but I get the idea. Regardless will this work even if the server is NATed? – John Sep 03 '09 at 00:39
  • 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 Sep 03 '09 at 01:03
  • 2
    No, it won't be correct if the server is NATed and only has an internal IP. – Simon East Nov 11 '15 at 08:19
12

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
  • self.location.hostname - Sets or retrieves the host name part of the location or URL.
Jamie Barker
  • 7,735
  • 3
  • 26
  • 62
hookenz
  • 30,814
  • 37
  • 149
  • 251
5

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.

5

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:

$.getJSON( "//freegeoip.net/json/" + window.location.host + "?callback=?", function(data) {
    console.warn('Fetching JSON data...');
    // Log output to console
    console.info(JSON.stringify(data, null, 2));
});

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

Community
  • 1
  • 1
Jiab77
  • 319
  • 3
  • 11
  • The question was about the server's IP, this will give you the client's. – Andris Jan 23 '18 at 17:46
  • Look at the js part... 'window.location.host' return the server name , so the service will use it and return server IP from client side... and not the client IP – Jiab77 Jan 23 '18 at 17:49
  • Ah, I see, you're right. Partially. I wanted to use this to check if the server I connect to is at the predefined IP, but resolving the hosname will give the official, and not the actual IP. – Andris Jan 23 '18 at 23:03
  • This API endpoint is deprecated and will stop work…sit: https://github.com/apilayer/freegeoip#readme" Do you have any other site? – Tenz May 11 '18 at 16:50
  • @Tenz I've just planed to signup on their site (ipstack.com) to get an api key. The service stay free anyway for the same given informations. They just give you more information if you pay their service. Sorry if my English is bad... – Jiab77 May 11 '18 at 17:03
  • what is the different between pay and unpaid? what do we have on the unpay and the pay, do you know? – Tenz May 11 '18 at 18:37
  • If you go on their site https://ipstack.com/ they describe their data as modules. The location module is free if I understood well but not all others. – Jiab77 May 11 '18 at 18:39
  • @Tenz, I've signed up and now using their API, just required minor changes like url, create an API key and only one JSON key name if I remember well, otherwise everything goes same. – Jiab77 Aug 06 '18 at 03:56
2

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
  • 1,511
  • 3
  • 15
  • 27
1

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.

tgkprog
  • 4,405
  • 4
  • 39
  • 66
  • FF should expose the main IP and list of IPs it used for various resources when getting a page - in the page options maybe or available to an extention. – tgkprog Apr 24 '14 at 12:13
0

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.

Community
  • 1
  • 1
M Juckes
  • 271
  • 1
  • 6
-1

<script type="application/javascript">
  function getIP(json) {
    document.write("My public IP address is: ", json.ip);
  }
</script>
<script type="application/javascript" src="http://ipinfo.io/?format=jsonp&callback=getIP"></script>
-4

I am sure the following code will help you to get ip address.

<script type="application/javascript">
    function getip(json){
      alert(json.ip); // alerts the ip address
    }
</script>

<script type="application/javascript" src="http://www.telize.com/jsonip?callback=getip"></script>
Rana Aalamgeer
  • 662
  • 2
  • 6
  • 22
  • Advising people to load a code snippet from a random source is a security nightmare. (Besides, as that site says, "The public API will permanently shut down on November 15th, 2015"). – hashchange Nov 07 '16 at 08:26
  • 1
    Also, the question was about the server's IP, not the client's. – Andris Jan 23 '18 at 17:46