I am working on this question.
Now I am trying to use getmac
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);
});
I get this error:
Error: Command failed: the command "getmac" could not be found
Do you know how to get this to work?
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 } ] }
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); }
On node ≥ 0.11 you are not required to use the asynchronous version:
var addr = require('node-macaddress').one();
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.