Page 1 of 1
Getting local IP address in javascript?
Posted: Sat Feb 19, 2011 11:47 am
by Bram
Is there a way to get the visitors *local* IP-address in javascript? I mean his LAN address like 192.168.1.x, not his regular IP-address.
I found several approaches that attempt to do this, but they either give me the external (regular) IP address, or they give 127.0.0.1 which doesn't really tell me anything. I need the local network address.
Just an alert saying "your machine's local network IP is 10.0.0.139" would be great.
Re: Getting local IP address in javascript?
Posted: Sat Feb 19, 2011 12:01 pm
by McInfo
It seems unlikely that the browser would leak information about a network interface other than the one it is using to connect to the server. That would be like allowing the server to see the file structure of the client.
Re: Getting local IP address in javascript?
Posted: Sat Feb 19, 2011 12:40 pm
by Bram
Well, strictly speaking, it's not 'leaking' anything to the server: I only need the IP address in Javascript, which is obviously local / client-side.
Re: Getting local IP address in javascript?
Posted: Sat Feb 19, 2011 12:45 pm
by McInfo
Any data JavaScript has access to can be sent back to the server.
Re: Getting local IP address in javascript?
Posted: Sat Feb 19, 2011 12:53 pm
by Bram
Well, this page seems to do something similar with Java instead of JavaScript:
http://www.auditmypc.com/anonymous-surfing.asp
Is this a security breach? And if it's possible with Java, isn't it with Javascript? (I mean because they both run locally, I know the two haven't got much else in common besides their name)
Re: Getting local IP address in javascript?
Posted: Sat Feb 19, 2011 1:19 pm
by McInfo
I don't see any Java on that page. All of that information either comes from headers that the browser sent when requesting the page or is based on data that can be found in the $_SERVER array in PHP.
- Browser (user agent) comes from $_SERVER['HTTP_USER_AGENT'].
- IP address comes from $_SERVER['REMOTE_ADDR'].
- Hostname comes from doing an nslookup on the IP address.
- Location information comes from using geolocation based on the IP address, which I have no experience with.
Re: Getting local IP address in javascript?
Posted: Mon Feb 21, 2011 2:23 am
by Bram
I actually DO see my internal network IP on that page, but only after a popup from my firewall about java.exe
Someone else suggested to call a static java package from within javascript, like this:
Code: Select all
var yip2=java.net.InetAddress.getLocalHost();
var yip=yip2.getHostAddress();
alert("your machine's local network IP is "+yip);
However this didn't work on my PC (even though I have the latest Sun Java installed).
Re: Getting local IP address in javascript?
Posted: Tue Feb 22, 2011 1:51 am
by Bram
rajiv wrote:
This will get the user's Ip address and hostname....
Unfortunately this gives the user's external IP and hostname only, which is trivial.
I need their internal, local network IP, like "10.x.x.x" or "192.168.x.x".
Re: Getting local IP address in javascript?
Posted: Tue Feb 22, 2011 2:09 am
by Weirdan
Bram wrote:However this didn't work on my PC (even though I have the latest Sun Java installed).
Not every browser allow for direct javascript -> java access. You may need to create an applet exposing the functionality you need.
Re: Getting local IP address in javascript?
Posted: Fri Feb 24, 2012 2:14 am
by isuchan
Hi all,
does anybody experienced to use geolocation in Mainland China?
I wonder whether there is a javascript script that can tell the user latitude and longitude which work when being used in mainland china. My plan is after I got the value of latitude and longitude I will feed them into Baidu API and get the location from there.
Many thanks.