使用getmac使用node.js获取当前计算机的mac地址 [英] get the mac address of the current machine with node.js using getmac

查看:1553
本文介绍了使用getmac使用node.js获取当前计算机的mac地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究此问题.

现在我正在尝试使用 getmac

使用node.js获取当前计算机的mac地址.

to get the mac address of the current machine with node.js.

我遵循了安装说明.但是,当我运行以下代码时:

I followed the installation instructions. But when I run this code:

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

我收到此错误:

错误:命令失败: 找不到命令"getmac"

Error: Command failed: the command "getmac" could not be found

您知道如何使它正常工作吗?

Do you know how to get this to work?

推荐答案

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

On NodeJS ≥ 0.11 the mac address for each network interface is in the output of os.networkInterfaces(), e.g.

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 (免责声明:我是上述软件包的作者).

In NodeJS ≤ 0.10 you need to find out the mac addresses on your own, but there are packages to help you with that: node-macaddress (disclaimer: I am the author of said package).

此软件包还为您的主机选择了一个接口,以便您可以执行

This package also selects one interface for your host so that you can do just

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

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

On node ≥ 0.11 you are not required to use the asynchronous version:

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

由于您通常只对"hosts macaddress"感兴趣(尽管不存在这样的问题,即主机可以具有多个网络接口,而每个网络接口都有一个单独的mac地址),因此此调用将为您提供确切的信息.

Since you are typically only interested in "the hosts macaddress" (although there is no such thing as a host can have multiple network interfaces each having an individual mac address), this call will give you exactly that.

这篇关于使用getmac使用node.js获取当前计算机的mac地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆