Skip to content

Instantly share code, notes, and snippets.

@paveltretyakovru
Created April 14, 2020 14:07
Show Gist options
  • Save paveltretyakovru/1bcb007d391d7aebaeb92e2e819b6063 to your computer and use it in GitHub Desktop.
Save paveltretyakovru/1bcb007d391d7aebaeb92e2e819b6063 to your computer and use it in GitHub Desktop.
Script to print current network interfaces and their ip addressess
'use strict';
var os = require('os');
var ifaces = os.networkInterfaces();
module.exports = () => {
const result = [];
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
result.push(iface.address);
} else {
// this interface has only one ipv4 adress
result.push(iface.address);
}
++alias;
});
});
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment