node.js获取本机Ip, hostName, mac

Posted on 2017-06-28 13:40  lianfeng  阅读(1349)  评论(0编辑  收藏  举报
//获取ip地址
getIPAdress() {
  let interfaces = require('os').networkInterfaces();
  for (var devName in interfaces) {
    var iface = interfaces[devName];
      for (var i = 0; i < iface.length; i++) {
        let alias = iface[i];
        if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
          console.log(alias.address);
        }
      }
  }
},

 

//获取hostName
getHostName() {
  let os = require('os');
  console.log(os.hostname())
},

 

//获取mac地址,首先要安装getmac依赖-----  npm install --save getmac
require('getmac').getMac(function(err,macAddress){
  console.log(macAddress)
})

 

Copyright © 2024 lianfeng
Powered by .NET 9.0 on Kubernetes