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?
No you cannot get the MAC address in JavaScript, mainly because the MAC address uniquely identifies the running computer so it would be a security vulnerability. Now if all you need is a unique identifier, I suggest you create one yourself using some cryptographic algorithm and store it in a cookie.
To see if Node is installed, type node -v in Terminal. This should print the version number so you'll see something like this v0. 10.31 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With