Skip to content

Instantly share code, notes, and snippets.

@maggiben
Created May 16, 2018 22:22
Show Gist options
  • Save maggiben/15b0d3a3756d7b8b0d3f9db0dd37ce80 to your computer and use it in GitHub Desktop.
Save maggiben/15b0d3a3756d7b8b0d3f9db0dd37ce80 to your computer and use it in GitHub Desktop.
node ip
var os = require('os');
var ifaces = os.networkInterfaces();
// from https://stackoverflow.com/questions/3653065/get-local-ip-address-in-node-js
Object.keys(ifaces).forEach(function (ifname) {
var alias = 0;
ifaces[ifname].forEach(function (iface) {
if ('IPv4' !== iface.family || iface.internal !== false) {
// skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
return;
}
if (alias >= 1) {
// this single interface has multiple ipv4 addresses
console.log(ifname + ':' + alias, iface.address);
} else {
// this interface has only one ipv4 adress
console.log(ifname, iface.address);
}
++alias;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment