我正在研究this question。
现在我正在尝试使用getmac
使用node.js获取当前计算机的mac地址。
我按照安装说明操作。但是当我运行这段代码时:
require('getmac').getMac(function(err,macAddress){
if (err) throw err;
console.log(macAddress);
});
我收到此错误:
错误:命令失败: 无法找到“getmac”命令
你知道如何让它发挥作用吗?
答案 0 :(得分:16)
在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();
由于您通常只对“主机macaddress”感兴趣(尽管没有主机可以有多个网络接口,每个网络接口都有一个单独的mac地址),这个调用将为您提供完全相同的。
答案 1 :(得分:4)
Node.JS脚本可以通过检查链路本地IPv6地址来发现当前计算机的MAC地址。 (需要注意的是:这要求IPv6堆栈在O / S中处于活动状态,这种情况越来越普遍)
e.g。
LL: fe80::0211:22ff:fe33:4455
MAC: 0011:22 33:4455
基于http://en.wikipedia.org/wiki/IPv6_address#Modified_EUI-64
在Windows上,必须通过运行提升的命令来停用randomizeidentifier:
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('-');
};
答案 2 :(得分:2)
您需要在命令提示符/终端中使用 npm install getmac 安装 getmac node_module。然后,它会工作。
答案 3 :(得分:1)
使用终端安装软件包 getmac 。
npm install --save getmac
在节点中使用
const getmac = require('getmac')
const callMac = () =>{
return getmac.default()
}
输出>>“ 00:05:1b:61:d3:05”
答案 4 :(得分:-2)
您必须使用root用户或命令运行它。