www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Getting local IP address in javascript?

  1. #1
    Join Date
    Feb 2011
    Posts
    4

    Arrow 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.

  2. #2
    Join Date
    Nov 2003
    Location
    Worthington, OH, USA
    Posts
    3,634
    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.
    54 68 65 42 65 61 72 4D 61 79

  3. #3
    Join Date
    Feb 2011
    Posts
    4
    Thanks for the reply. I'm not neccessarily grabbing the internal address from outside, 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

  4. #4
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by Bram_vG View Post
    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.
    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())
    //shows: your machine's local network IP is 192.17.145.155

  5. #5
    Join Date
    Feb 2011
    Posts
    4
    Quote Originally Posted by rnd me View Post
    with a little help from java:
    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:

    Code:
    <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

  6. #6
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    all i know is that both snips work for me.
    tested in FF3.6

  7. #7
    Join Date
    Feb 2011
    Posts
    4
    Quote Originally Posted by rnd me View Post
    all i know is that both snips work for me.
    tested in FF3.6
    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 ?

  8. #8
    Join Date
    Nov 2011
    Posts
    2
    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

  9. #9
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
    Code:
    Youre = {
          STILL_not_getting_it:function(){
               alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
          },
          MissingThePoint:function(msg){
                alert("You're missing the point. " + msg);
          }
    }
    Youre.STILL_not_getting_it();

  10. #10
    Join Date
    Dec 2011
    Location
    White House
    Posts
    1

    Yes it is possible

    Quote Originally Posted by Bram_vG View Post
    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.
    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. Now you have something to show when you explain your question to other people.

    Quote Originally Posted by Azzdog dev View Post
    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.
    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.

  11. #11
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    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']."');");
    ?>
    Last edited by 007Julien; 12-24-2011 at 12:38 PM.

  12. #12
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    I have include a iptocountry php file which give now the country... Thank you for indicating me of possible errors.

  13. #13
    Join Date
    Nov 2002
    Location
    Flint, Michigan, USA
    Posts
    588
    You may have to use a secure (httpS://...) conection to get what you need.

  14. #14
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Quote Originally Posted by 007Julien View Post
    I have include a iptocountry php file which give now the country... Thank you for indicating me of possible errors.
    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.
    We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
    Code:
    Youre = {
          STILL_not_getting_it:function(){
               alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
          },
          MissingThePoint:function(msg){
                alert("You're missing the point. " + msg);
          }
    }
    Youre.STILL_not_getting_it();

  15. #15
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    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...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles