Skip to content

Instantly share code, notes, and snippets.

@huzemin
Created January 21, 2016 05:03
Show Gist options
  • Save huzemin/09302ef463202084073a to your computer and use it in GitHub Desktop.
Save huzemin/09302ef463202084073a to your computer and use it in GitHub Desktop.
获取本机IP
var os = require('os');
// 获取网卡信息
var networkInterfaces = os.networkInterfaces();
function getHostIp() {
// 可能会用多个活动网卡的情况,这样也会出现多个IP
var ip = [];
for(var key in networkInterfaces) {
// 本地环路
if(key == 'lo') {
continue;
}
// 获取有效网卡的IP
networkInterfaces[key].forEach(function(detail) {
if(detail.family == 'IPv4' && !detail.internal) {
ip.push(detail.address);
}
});
}
return ip;
}
module.exports = getHostIp();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment