Skip to content

Instantly share code, notes, and snippets.

@geekykant
Created April 18, 2019 09:22
Show Gist options
  • Save geekykant/5420f87cbe50735a9081d2e85409875b to your computer and use it in GitHub Desktop.
Save geekykant/5420f87cbe50735a9081d2e85409875b to your computer and use it in GitHub Desktop.
Get local IP address on a network using JS.
window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;//compatibility for Firefox and chrome
var pc = new RTCPeerConnection({iceServers:[]}), noop = function(){};
pc.createDataChannel('');//create a bogus data channel
pc.createOffer(pc.setLocalDescription.bind(pc), noop);// create offer and set local description
pc.onicecandidate = function(ice)
{
if (ice && ice.candidate && ice.candidate.candidate)
{
var myIP = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/.exec(ice.candidate.candidate)[1];
pc.onicecandidate = alert(`IP Address: ${myIP}`);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment