/    Sign up×
Community /Pin to ProfileBookmark

Getting local IP address in javascript?

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.

to post a comment
JavaScript

16 Comments(s)

Copy linkTweet thisAlerts:
@TheBearMayFeb 19.2011 — To get the private network address (10.x.x.x, 192.168.x.x, etc) the user will need to be able to hit a web server inside the private network without being routed outside. In short, if you're trying to grab someones private network address from outside that network you can't.
Copy linkTweet thisAlerts:
@Bram_vGauthorFeb 19.2011 — Thanks for the reply. I'm not neccessarily grabbing the internal address [b]from outside[/b], I only need Javascript (which is obviously local/inside) to determine it.

Once I have the address in JS, I can of course fill it in some form input field or do a XmlHttpRequest or whatever to process it further.

Is there some way for javascript to detect the local IP address? I've seen some attempts with socket connections or similar approaches, but all in vain ?
Copy linkTweet thisAlerts:
@rnd_meFeb 19.2011 — 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.[/QUOTE]


with a little help from java:
[CODE]function myIP(){ var vi="uses java to get the users local ip number"
var yip2=java.net.InetAddress.getLocalHost();
var yip=yip2.getHostAddress();
return yip;
}//end myIP

alert("your machine's local network IP is "+ myIP())
[/CODE]


//shows: your machine's local network IP is 192.17.145.155
Copy linkTweet thisAlerts:
@Bram_vGauthorFeb 21.2011 — with a little help from java:[/QUOTE]
Hey, thanks, look promising, however this doesn't seem to work on my PC? (I do have the latest Sun Java installed)

Just to be sure, I'd have to include that as regular javascript, right? This is the exact html file I tried:

<html><head><script type='text/javascript'>
var yip2=java.net.InetAddress.getLocalHost();
var yip=yip2.getHostAddress();
alert("your machine's local network IP is "+yip);
</script></head><body>test</body></html>


Furthermore I also came accross this page:

http://www.auditmypc.com/anonymous-surfing.asp

This DOES show my local IP, but only after a popup from my firewall about java.exe
Copy linkTweet thisAlerts:
@rnd_meFeb 21.2011 — all i know is that both snips work for me.

tested in FF3.6
Copy linkTweet thisAlerts:
@Bram_vGauthorFeb 22.2011 — all i know is that both snips work for me.

tested in FF3.6[/QUOTE]

Hmm interesting, I tried on Firefox as well (used Opera before) and now it does give me something: 127.0.0.1 ?

Any idea how to get it to display the actual internal IP, I mean like 10.x.x.x or 192.168.x.x ?
Copy linkTweet thisAlerts:
@Azzdog_devNov 20.2011 — You cannot get local (internal) ip address. Networks are almost ALWAYS configured to hide internal ip, and thus you can only get the external (the one set by ISP) ip. Don't know the code to do this though.

If your windows user, open command prompt and type ipconfig

This will NOT change anything because you haven't told it to. It simply shows your current ip configuration. Write down the ip address. Now go to google and search 'what is my ip'. This gives you your external ip
Copy linkTweet thisAlerts:
@PaardekutDec 24.2011 — 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. [/QUOTE]

When you go to this website, you will see your own IP address. Right below that IP address you see:

LAN: Get my LAN address [button/link]

Click on "Get my LAN address" and you will see an IP like 192.168.0.x or something similar. [B]Now you have something to show when you explain your question to other people.[/B]

You cannot get local (internal) ip address. Networks are almost ALWAYS configured to hide internal ip, and thus you can only get the external (the one set by ISP) ip.[/QUOTE]
Then how come this website did manage to show my internal IP address? JavaScript works locally / internal and internal IP address are not hidden to internal applications.
Copy linkTweet thisAlerts:
@007JulienDec 24.2011 — It's possible with this script witch load dynamicaly a javascript build with php.
[CODE]<?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Content-Type:text/javascript;charset=utf-8");

if (!empty($_GET)) {
// lecture du GET et execution des tâches


}
exit("alert('Votre adresseIP est : ".$_SERVER['REMOTE_ADDR']."');");
?>[/CODE]
Copy linkTweet thisAlerts:
@007JulienDec 24.2011 — I have include a iptocountry php file which give now the country... Thank you for indicating me of possible errors.
Copy linkTweet thisAlerts:
@jalarieDec 30.2011 — You may have to use a secure (httpS://...) conection to get what you need.
Copy linkTweet thisAlerts:
@JunkMaleDec 31.2011 — I have include a iptocountry php file which give now the country... Thank you for indicating me of possible errors.[/QUOTE]

Which is flawed because this method of IP address to geo location is not 100% accurate.

When I was a BT Internet customer, the ISP for BT Internet was Yahoo who uses servers in the USA and therefore all the UK Yahoo ISP users have an IP address block in the USA when they are in the UK. That is just one ISP and one customer amongst millions.

Addressing of blocks is not sequential, a country will not have a single contiguous block, it will have 100's to 1000's of blocks that are allocated in small sequences.

A proxy server breaks any ip to geo location detection.

I will give you an idea of how far off geo location by this method is, Yahoo and other sites seem to think I live in Maidenhead, Kent. Nope, sorry, never lived in Kent, I have an Aunt that lives near to Maidenhead that I haven't seen in 36 years, but I don't live in Maidenhead, I live about 2 hours drive north in the East Midlands.

Word to the wise, do not rely on this method of fixing your users location.
Copy linkTweet thisAlerts:
@007JulienDec 31.2011 — Thank you for this information Junk Male, even if everybody has no aunt to Maidenhead, we shall retain that the geolocalisation is uncertain. It's like the bikini. It gives ideas even if it can hide the main part...
Copy linkTweet thisAlerts:
@alexemil5Jan 03.2013 — Junk male.. Thanks for the link.. There are allots of info, Which I need...
Copy linkTweet thisAlerts:
@proforprofessorAug 01.2013 — This code will work, I've also struggled with the same thing but now i got it ?

<script type="text/javascript">

function openall()

{

var strComputer = ".";

var SWBemlocator = new ActiveXObject("WbemScripting.SWbemLocator");

var objWMIService = SWBemlocator.ConnectServer(strComputer, "/root/CIMV2");

var strProcess = "";

var colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem");

var e = new Enumerator(colItems);

for(; ! e.atEnd(); e.moveNext())

{

strProcess += "";

}

colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=true");

e = new Enumerator(colItems);

for(; ! e.atEnd(); e.moveNext())

{

strProcess +=e.item().IPAddress(0)+ "n";

}

document.getElementById ('outip').value = strProcess;

}

</script>

<script type = 'text/javascript'>

<body onload = "openall();"

<input name="outip" type="text" id="outip" value="" size="50">

</body>

For all your IT related questions, visit my facebook page https://www.facebook.com/pages/Once-a-Hacker-Always-a-Hacker/187436111432160
×

Success!

Help @Bram_vG spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...