Getting local IP address in javascript?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Bram
Forum Newbie
Posts: 8
Joined: Sat Feb 19, 2011 11:45 am

Getting local IP address in javascript?

Post 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.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Getting local IP address in javascript?

Post 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.
Bram
Forum Newbie
Posts: 8
Joined: Sat Feb 19, 2011 11:45 am

Re: Getting local IP address in javascript?

Post 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.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Getting local IP address in javascript?

Post by McInfo »

Any data JavaScript has access to can be sent back to the server.
Bram
Forum Newbie
Posts: 8
Joined: Sat Feb 19, 2011 11:45 am

Re: Getting local IP address in javascript?

Post 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)
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Getting local IP address in javascript?

Post 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.
Bram
Forum Newbie
Posts: 8
Joined: Sat Feb 19, 2011 11:45 am

Re: Getting local IP address in javascript?

Post 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).
Bram
Forum Newbie
Posts: 8
Joined: Sat Feb 19, 2011 11:45 am

Re: Getting local IP address in javascript?

Post 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".
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Getting local IP address in javascript?

Post 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.
isuchan
Forum Newbie
Posts: 1
Joined: Fri Feb 24, 2012 2:01 am

Re: Getting local IP address in javascript?

Post 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.
Post Reply