Skip to content

Instantly share code, notes, and snippets.

@aleung
Last active November 5, 2015 03:43
Show Gist options
  • Save aleung/acfa5ca91c8de6764280 to your computer and use it in GitHub Desktop.
Save aleung/acfa5ca91c8de6764280 to your computer and use it in GitHub Desktop.
Get local IP address of specific NIC in node.js
var _ = require('lodash');
var os = require('os');
function getIpAddr(nicName) {
let ifaces = os.networkInterfaces();
let iface = _.find(ifaces[nicName], iface => { return iface.family === 'IPv4' });
return _.result(iface, 'address');
}
module.exports = _.memoize(getIpAddr);
var ip = require('./ipaddr');
console.log(ip('eth0'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment