Created
January 21, 2016 05:03
-
-
Save huzemin/09302ef463202084073a to your computer and use it in GitHub Desktop.
获取本机IP
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'); | |
// 获取网卡信息 | |
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