Created
April 18, 2016 08:53
-
-
Save eplawless/b0a0910dd61211c22a616d649ea8cbce to your computer and use it in GitHub Desktop.
Get all local host addresses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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