In Focus

Getting Client IP Address Or Local IP Address In JavaScript

Have a look at this blog to learn how can we get the local IP address of a user in JavaScript.

Getting Client IP Address in ASP.NET using JavaScript

 
So, it all started with a requirement for my website when I needed to get the client's IP address for some security purposes. It felt like an easy task in the beginning but after some time, I realized that most of the available methods of getting an IP address return the Server's IP Address and not the local IP address of the client. And, there are some APIs which return your Public IP address but again, none of them returns the local IP address. So, we will see how we can achieve that.
 

Using JavaScript

 
In JavaScript, you can use the following script for getting the IP address.
  1. < script >  
  2.     var RTCPeerConnection = /*window.RTCPeerConnection ||*/ window.webkitRTCPeerConnection || window.mozRTCPeerConnection;  
  3. if (RTCPeerConnection)(function() {  
  4.     var rtc = new RTCPeerConnection({  
  5.         iceServers: []  
  6.     });  
  7.     if (1 || window.mozRTCPeerConnection) {  
  8.         rtc.createDataChannel('', {  
  9.             reliable: false  
  10.         });  
  11.     };  
  12.     rtc.onicecandidate = function(evt) {  
  13.         if (evt.candidate) grepSDP("a=" + evt.candidate.candidate);  
  14.     };  
  15.     rtc.createOffer(function(offerDesc) {  
  16.         grepSDP(offerDesc.sdp);  
  17.         rtc.setLocalDescription(offerDesc);  
  18.     }, function(e) {  
  19.         console.warn("offer failed", e);  
  20.     });  
  21.     var addrs = Object.create(null);  
  22.     addrs["0.0.0.0"] = false;  
  23.   
  24.     function updateDisplay(newAddr) {  
  25.         if (newAddr in addrs) return;  
  26.         else addrs[newAddr] = true;  
  27.         var displayAddrs = Object.keys(addrs).filter(function(k) {  
  28.             return addrs[k];  
  29.         });  
  30.         document.getElementById('list').textContent = displayAddrs.join(" or perhaps ") || "n/a";  
  31.     }  
  32.   
  33.     function grepSDP(sdp) {  
  34.         var hosts = [];  
  35.         sdp.split('\r\n').forEach(function(line) {  
  36.             if (~line.indexOf("a=candidate")) {  
  37.                 var parts = line.split(' '),  
  38.                     addr = parts[4],  
  39.                     type = parts[7];  
  40.                 if (type === 'host') updateDisplay(addr);  
  41.             } else if (~line.indexOf("c=")) {  
  42.                 var parts = line.split(' '),  
  43.                     addr = parts[2];  
  44.                 updateDisplay(addr);  
  45.             }  
  46.         });  
  47.     }  
  48. })();  
  49. else {  
  50.     document.getElementById('list').innerHTML = "<code>ifconfig| grep inet | grep -v inet6 | cut -d\" \" -f2 | tail -n1</code>";  
  51.     document.getElementById('list').nextSibling.textContent = "In Chrome and Firefox your IP should display automatically, by the power of WebRTCskull.";  
  52. } < /script>   

Just paste this script in your JavaScript code and add this element into your body tag.

  1. <body>  
  2.    <div id="list"></div>  
  3. </body>  
Now, just run the code and this will get you your Client or Local IP address.
 

Getting Public IP address

 
There are two types of IP addresses - Local and Public. The above script is for getting the Client or Local IP Address but for getting the Public IP Address, you can use this script.
  1. function getIP(json) {  
  2.     document.write("My public IP address is: ", json.ip);  
  3.  }  
  4. <script type="application/javascript" src="https://api.ipify.org?format=jsonp&callback=getIP"></script>  
This will return your Public IP Address.