【问题标题】:get the mac address of the current machine with node.js using getmac使用getmac获取node.js当前机器的mac地址
【发布时间】:2013-04-29 11:28:20
【问题描述】:

我正在处理this question

现在我正在尝试使用getmac

用node.js获取当前机器的mac地址。

我按照安装说明进行操作。但是当我运行这段代码时:

require('getmac').getMac(function(err,macAddress){
    if (err)  throw err;
    console.log(macAddress);    
});

我得到这个错误:

错误:命令失败: 找不到命令“getmac”

你知道如何让它工作吗?

【问题讨论】:

  • 呃...什么平台以及如何运行它?
  • 你安装了模块吗? npm install getmac ?
  • @JoachimIsaksson 节点版本 0.8.16
  • 你有什么版本的 Windows?从错误中,它期望您有一个 getmac 命令,这显然是缺失的。谷歌搜索说它应该适用于 XP 和 7,但我不知道适用于 Windows 8。
  • 你们真的让它工作了吗?或者这个 getmac 模块中是否存在错误? @loganfsmyth 感谢您的回答,我使用 XP

标签: node.js mac-address


【解决方案1】:

使用终端安装包getmac

npm install --save getmac

在节点中使用

const getmac = require('getmac')

const callMac = () =>{
    return getmac.default()
}

输出 >> "00:05:1b:61:d3:05"

【讨论】:

    【解决方案2】:

    您需要在命令提示符/终端中使用 npm install getmac 安装 getmac node_module。然后,它会工作。

    【讨论】:

      【解决方案3】:

      在 NodeJS ≥ 0.11 上,每个网络接口的 mac 地址在 os.networkInterfaces() 的输出中,例如

      require('os').networkInterfaces()
      
      { eth0: 
         [ { address: 'fe80::cae0:ebff:fe14:1dab',
             netmask: 'ffff:ffff:ffff:ffff::',
             family: 'IPv6',
             mac: 'c8:e0:eb:14:1d:ab',
             scopeid: 4,
             internal: false },
           { address: '192.168.178.22',
             netmask: '255.255.255.0',
             family: 'IPv4',
             mac: 'c8:e0:eb:14:1d:ab',
             internal: false } ] }
      

      在 NodeJS ≤ 0.10 中,您需要自己找出 mac 地址,但有一些软件包可以帮助您:node-macaddress(免责声明:我是该软件包的作者)。

      这个包还为你的主机选择了一个接口,这样你就可以做到了

      require('node-macaddress').one(function (err, addr) { console.log(addr); }
      

      在节点 ≥ 0.11 上,您不需要使用异步版本:

      var addr = require('node-macaddress').one();
      

      由于您通常只对“主机 MAC 地址”感兴趣(尽管没有这样的事情,即主机可以有多个网络接口,每个接口都有一个单独的 MAC 地址),此调用将准确地为您提供。

      【讨论】:

      • 这在 8.0.0 之后停止工作。目前 8.1.3 仍然为设备中的 mac 返回零(不是本地的)。它需要修补,直到他们恢复他们的 8.0.0 代码?! ip addr 会给出一个链接/以太,但其他的会更好。
      • 你能帮我解决这个问题吗stackoverflow.com/questions/52241799/…@scravy
      • @MasterJames 这个问题早就修复了。
      【解决方案4】:

      Node.JS 脚本可以通过检查链路本地 IPv6 地址来发现当前机器的 MAC 地址。 (警告:这要求 IPv6 堆栈在 O/S 中处于活动状态,这越来越普遍)

      例如

      LL: fe80::0211:22ff:fe33:4455
      MAC:      0011:22     33:4455
      

      基于http://en.wikipedia.org/wiki/IPv6_address#Modified_EUI-64

      1. 把中间的0xfffe去掉
      2. 使用 XOR 0x020000000000 按位反转 bit#41

      在 Windows 上,必须通过运行提升的命令来停用 randomizeidentifiers:

      netsh interface ipv6 set global randomizeidentifiers=disabled
      

      以下代码使用此技术生成 Variant 1 UUID(尾生成只发生一次):

      function generateUUID() {
          generateUUID.tail = generateUUID.tail || (function(nics) {
              var nic, index, addr, retn;
              for (nic in nics) { // try to obtain the MAC address from the IPv6 scope-local address
                  for (index in nics[nic]) {
                      addr = nics[nic][index];
                      if (!addr.internal) {
                          if (addr.address.indexOf('fe80::') === 0) { // found scope-local
                              retn = retn || addr.address.slice(6).split(/:/).map(function(v, i, a) {
                                  return parseInt(v, 16);
                              });
                          }
                      }
                  }
              }
              if (!retn) { // no IPv6 so generate random MAC with multicast bit set
                  index = Math.pow(2, 16);
                  retn = [];
                  retn.push(Math.floor(Math.random() * index) | 0x1000); // set multicast bit
                  retn.push(Math.floor(Math.random() * index));
                  retn.push(Math.floor(Math.random() * index));
                  retn.push(Math.floor(Math.random() * index));
              }
              retn[3] = 0x10000 | retn[3];
              retn[2] = 0x10000 | retn[1] & 0xff00 | retn[2] & 0x00ff; // eliminate FFFE from xxxx:xxFF:FExx:xxxx
              retn[1] = 0x10000 | retn[0] ^ 0x0200; // invert bit#41
              retn[0] = 0x18000 | process.pid & 0x3fff;
              retn = retn.map(function(v, i, a) {
                  return v.toString(16).slice(1)
              });
              return retn[0] + '-' + retn[1] + retn[2] + retn[3];
          })(require('os').networkInterfaces());
      
          var head = process.hrtime(), now = Math.floor(Date.now() / 1000);
          head[1] = Math.floor(head[1] * 0.268435456); // 2^28 / 10^9
          head[2] = (0x11000 | head[1] & 0x0fff).toString(16).slice(1);
          head[1] = (0x10000 | head[1] >>> 12 & 0xffff).toString(16).slice(1);
          head[0] = (4294967296 + now).toString(16).slice(1);
          return head.concat(generateUUID.tail).join('-');
      };
      

      【讨论】:

        【解决方案5】:

        您必须使用 root 用户或命令运行它。

        【讨论】:

        • 您没有提供任何示例、脚本或准确的建议,并且 以 root 身份运行对于获取计算机的 MAC 地址并不是完全必要的。
        猜你喜欢
        • 2012-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-28
        相关资源
        最近更新 更多