SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,832
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Bram_vG View Post
    I need the local network address.
    The only language I know of that JavaScript can use to get IP addresses that might have access to be able to get that one is Java. You'd need the Java to be running in the browser for it to have access to that IP address.

    I don't know if Java has access to that address to be able to pass it to JavaScript but it is certain that none of the languages running on the server that can pass an IP address to JavaScript will be able to get that address so Java is the only possibility.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply. Yes, certainly no server-sided language or script will ever be able to get my local IP, so that would leave Java or Javascript the only possible candidates.

    Someone else suggested a method for calling static Java packages from within Javascript, but that didn't seem to work so far.

    Would you (or anyone else) have any idea how to go about this?? I'm really stuck with this...

    I do have the latest Sun Java installed so that shouldn't be a problem.

  4. #4
    SitePoint Addict bronze trophy AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    298
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

  5. #5
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AllanP View Post
    Unfortunately this only gives the external (regular / global) IP, which is trivial.

    I need their internal, local network IP, like "10.x.x.x" or "192.168.x.x", any idea how to get that?

    It doesn't neccessarily have to be javascript, any other language would be fine too (although it'd have to run client-side obviously, so that'd narrow it down to JS or Java I guess).

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,832
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by AllanP View Post
    I just tried that code and it did NOT give the correct IP address. It told me the IP address of my internet connection and not that of my computer. That's because it is using server side code to get the IP address - SSI in that example.

    The only way that you might be able to get the local IP address is via Java running in the browser. JavaScript itself has no access to get the IP address and neither does anything running on the server.

    The following JavaScript call to Java retrieves an IP address but I don't think it iis the one you want but at least you might be able to figure out what modification is needed to this to retrieve the IP address that you actually want.

    Code:
    if (java && java.net)
    ip = ''+java.net.InetAddress.getLocalHost().getHostAddress();
    else ip = 'unknown';
    Alternatively, I just found out that it is possible to do it entirely with Java - as http://www.proxyblind.org/javaipp.shtml demonstrates.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Tags for this Thread

Bookmarks

Posting Permissions

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