16

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?

Community
  • 1
  • 1
Bud Wieser
  • 263
  • 1
  • 3
  • 8
  • Er... what platform and how are you running it? – Joachim Isaksson May 05 '13 at 16:13
  • Did you install the module? `npm install getmac` ? – drinchev May 05 '13 at 17:00
  • @JoachimIsaksson node version 0.8.16 – Bud Wieser May 05 '13 at 18:17
  • What version of Windows do you have? From the error, it is expecting you to have a `getmac` command, which is apparently missing. Googling around says it should be there for XP and 7, but I can't tell for Windows 8. – loganfsmyth May 05 '13 at 18:37
  • Do you guys actually get this to work or is there a bug in this getmac module? @loganfsmyth thanks for the answer, I use XP – Bud Wieser May 05 '13 at 19:29
  • I don't have a copy of Windows XP to test on, sorry. It is trying to run this command: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/getmac.mspx?mfr=true but is not able to find it. Either you don't have it installed, or your path set up so it cannot find it. – loganfsmyth May 05 '13 at 19:41
  • @loganfsmyth thanks this really helped. As I am trying to get the mac address of any machine running the server, no matter which OS, this getmac module maybe won't do the job as it seems to depend on the OS or the path set up like you said. – Bud Wieser May 06 '13 at 06:48
  • There is also: https://www.npmjs.com/package/node-macaddress which also works with Linux/Windows/OSX – scravy Mar 31 '15 at 02:16

5 Answers5

25

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.

scravy
  • 11,904
  • 14
  • 72
  • 127
  • This stopped working right after 8.0.0. currently 8.1.3 is still returning zeros for the mac in the device (not local). It needs patching until they get their 8.0.0 code back in order?! ip addr will give a link/ether but something else would be better. – Master James Jul 05 '17 at 02:21
  • can you please help me to solve this https://stackoverflow.com/questions/52241799/failed-to-compile-node-modules-macaddress-lib-windows-js-in-angular-5 @scravy – Zhu Sep 09 '18 at 06:07
  • @MasterJames this has long been fixed. – scravy Jul 10 '20 at 04:10
5

A Node.JS script can discover the MAC address of the current machine by examining the link-local IPv6 address. (caveat: this requires the IPv6 stack to be active within the O/S, which is increasingly common)

e.g.

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

Based on http://en.wikipedia.org/wiki/IPv6_address#Modified_EUI-64

  1. remove the 0xfffe from the middle
  2. bitwise invert bit#41 using XOR 0x020000000000

On Windows it is necessary to deactivate randomizeidentifiers by running the elevated command:

netsh interface ipv6 set global randomizeidentifiers=disabled

The following code uses this technique to generate a Variant 1 UUID (tail generation happens just once):

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('-');
};
user3560651
  • 51
  • 1
  • 3
2

You need to install getmac node_module using npm install getmac in command prompt/terminal. Then, it will work.

pavani sri
  • 547
  • 4
  • 6
2

install the package getmac using the terminal.

npm install --save getmac

using in node

const getmac = require('getmac')

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

output >> "00:05:1b:61:d3:05"

Jose Fdo
  • 91
  • 2
  • 8
-2

You must run it with root user or command.

Emre
  • 25
  • 1
  • 5
  • 2
    You didn't provide any example, script or accurate suggestion and *run it as root* isn't quite neccessary for getting MAC address of a computer. – ArchNoob Sep 30 '17 at 11:10