Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out the server IP address (using JavaScript) that the browser is connected to?

Tags:

javascript

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?

like image 203
John Avatar asked Sep 02 '09 23:09

John


People also ask

How do I get the IP of a website using JavaScript?

For example: json(`https://api.ipdata.co?api-key=${apiKey}&fields=ip`).then(data => { console. log(data. ip); });

How do I get an IP address in HTML?

php echo "Your IP is"; echo $_SERVER["REMOTE_ADDR"]; function get_ip_address() { // check for shared internet/ISP IP if (! empty($_SERVER['HTTP_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_CLIENT_IP'])) return $_SERVER['HTTP_CLIENT_IP']; // check for IPs passing through proxies if (!


2 Answers

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

like image 88
Spacemonkey Avatar answered Sep 19 '22 16:09

Spacemonkey


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.

like image 42
Jason Berry Avatar answered Sep 18 '22 16:09

Jason Berry