Created
February 5, 2020 20:18
-
-
Save jwoo92/082e1adf64b624b42e48f254976b193f to your computer and use it in GitHub Desktop.
Get all IP address of the server
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
/** | |
* Get all IP address of the server | |
* @param {Object|undefined} [{}] - An object. | |
* @param {Boolean} [obj.skipLocalhost=false] - Determines if the localhost address is returned in the result list | |
* @return {Array} Array of IPs | |
*/ | |
const getListOfIPV4Address = ({ skipLocalhost = false } = {}) => { | |
const os = require('os'); | |
const ifaces = os.networkInterfaces(); | |
let result = []; | |
Object.keys(ifaces).forEach(function(ifname) { | |
ifaces[ifname].forEach(function(iface) { | |
// skip non-ipv4 addresses | |
if ('IPv4' !== iface.family) { | |
return; | |
} | |
if (skipLocalhost) { | |
// skip over internal (i.e. 127.0.0.1) | |
if (iface.internal !== false) { | |
return; | |
} | |
} | |
result.push(iface.address); | |
}); | |
}); | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment