Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-server side method of getting local IP address in browser?

Tags:

ip-address

I am trying to provide a way for users on my LAN to "register" with the Network admin (me) without having to either a) host a page on my computer b) host a script on the central server (since it is only a router, not really a solid HTTP server) or c) sign up for a Dynamic Domain in order to either either of the first two and avoid the confusion of sending out a URL to a link to a local IP.

Is there a simple way to display the local IP address on screen via a client-side script? I'm thinking maybe I could have an iframe that points to some generic url with some javascript in the path, so that I can have the users go to a non-local site, and the iframe would pop up with their IP address that they can then enter into a form in the main remote page.

If all else fails, is there a way for them to look up their IP that is cross-platform and doesn't involve using the command line (I think the first, even if impossible, is probably more realistic than the second).

like image 943
Anthony Avatar asked Jan 26 '10 09:01

Anthony


People also ask

How do I find my local IP browser?

Use the ipconfig command Now that you have a Command Prompt open, simply type the command ipconfig into it and then press the Enter key. The Internet Protocol Configuration tool will now run and show you some information about your local network connection.

How do I find my non local IP address?

1. Go to Settings > WiFi > tap on your WiFi Network Name. 2. On the next screen, your Private IP Address will be listed next to IP Address Entry under “IPV4 ADDRESS” section.

How do I find someone's local IP address?

Use an IP lookup tool Starting with the simplest way to find someone's IP address is to use one of the many IP lookup tools available online. Resources such as WhatIsMyIPAddress.com or WhatIsMyIP.com offer tools to enter an IP address and search for its free public registry results.


2 Answers

Thankfully to Webrtc it is possible to access local IP from javascript.

Take a look at: http://net.ipcalf.com/ (source)

( It works in chrome )

However accessing local IP from Javascript could be a privacy and security issue.

and below simpler example then net.ipcalf.com

and on the jsfiddle

var RTCPeerConnection = window.webkitRTCPeerConnection || window.mozRTCPeerConnection;

var configuration = { "iceServers": [] };
var pc;
var localIP;

if(RTCPeerConnection){  

    pc = new RTCPeerConnection(configuration);
    pc.onicecandidate = function (evt) {  
        if (evt.candidate) { 
            if (!localIP) { 
                localIP = getIpFromString(  evt.candidate.candidate );
                console.log(localIP);
            }
        }
    };

     pc.createOffer(function (offerDesc) {;
        pc.setLocalDescription(offerDesc);                                      
    }, function (e) { console.warn("offer failed", e); });

    function getIpFromString(a)
    {
        var r = a.match(/\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/);
        return r[0];
    }

} else {
 //browser doesn't support webrtc   
}
like image 73
Pawel Wodzicki Avatar answered Sep 23 '22 11:09

Pawel Wodzicki


Yes there is actually. I think there is an example of what you are trying to do here: http://www.whatsmyip.org/more/

if you view source on that page you see the following for the internal ip section:

                            <p><b>Internal (LAN) IP</b>: <span id="localip">Checking...</span>
                        <script>
                            function MyAddress(IP)
                                { document.getElementById("localip").innerHTML = IP; } 
                        </script>
                        <applet code="MyAddress.class" MAYSCRIPT width=0 height=0>
                            Sorry You Need Java For This To Work
                        </applet>

So I'm thinking you have a couple of options, you can either download the copy of the compiled java applet on that site, or write your own. The general idea of how this works, is the java applets loads read the local ip, and then calls into the DOM and updates the innterHTML of that element. (btw, i'm not telling you to steal the applet, but it's a suggestion if you wanted to try it out for educational purposes)

Hope this helps. Mark

like image 32
Mark At Ramp51 Avatar answered Sep 23 '22 11:09

Mark At Ramp51