Skip to content

Instantly share code, notes, and snippets.

@eplawless
Created April 18, 2016 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eplawless/b0a0910dd61211c22a616d649ea8cbce to your computer and use it in GitHub Desktop.
Save eplawless/b0a0910dd61211c22a616d649ea8cbce to your computer and use it in GitHub Desktop.
Get all local host addresses
var os = require('os');
function getLocalHosts() {
var hosts = ['0.0.0.0', '::'];
var interfaces = os.networkInterfaces();
Object.keys(interfaces).forEach(function(interfaceName) {
var interface = interfaces[interfaceName];
interface.forEach(function(info) {
var address = info.address;
// append the interface name onto link-local IPv6 addresses
// http://stackoverflow.com/a/34275391
if (address.indexOf('fe80:') === 0) {
address += '%' + interfaceName;
}
hosts.push(address);
});
});
return hosts;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment