11

I would like to know if there is a way of having NODE retrieve the MAC address(es) of the server on which it is running.

clayperez
  • 807
  • 1
  • 9
  • 18
  • See [this answer](http://stackoverflow.com/questions/16386371/get-the-mac-address-of-the-current-machine-with-node-js-using-getmac) for another suggestion. – user3560651 Apr 22 '14 at 14:03
  • This package does it in a cross-platform way and is tested on node 0.10-0.12 as well as on io.js: https://github.com/scravy/node-macaddress - see the `one()` method. – scravy Mar 31 '15 at 08:08
  • Try this one: https://www.npmjs.com/package/ee-machine-id – camposer Mar 15 '17 at 20:40

5 Answers5

7

Node has no built-in was to access this kind of low-level data.

However, you could execute ifconfig and parse its output or write a C++ extension for node that provides a function to retrieve the mac address. An even easier way is reading /sys/class/net/eth?/address:

var fs = require('fs'),
    path = require('path');
function getMACAddresses() {
    var macs = {}
    var devs = fs.readdirSync('/sys/class/net/');
    devs.forEach(function(dev) {
        var fn = path.join('/sys/class/net', dev, 'address');
        if(dev.substr(0, 3) == 'eth' && fs.existsSync(fn)) {
            macs[dev] = fs.readFileSync(fn).toString().trim();
        }
    });
    return macs;
}

console.log(getMACAddresses());

The function returns an object containing the mac addresses of all eth* devices. If you want all devices that have one, even if they are e.g. called wlan*, simply remove the dev.substr(0, 3) == 'eth' check.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • 1
    ThiefMaster, thanks for the quick response. Is "/sys/class/net/eth?/address" cross-platform? – clayperez Jun 23 '12 at 21:37
  • 1
    Most likely linux-only. It certainly won't work on windows, not sure about other OSes such as *BSD. – ThiefMaster Jun 23 '12 at 21:38
  • I was initially using the getmac module however it will just return the mac address from the first device that it finds. In my case, I required the mac address of wlan0 so was able to easily use your code to get the mac address of wlan0 – Lucas Mar 06 '14 at 19:44
  • 1
    Node ≥ 0.11 as well as iojs will return mac addresses in the output of `os.networkInterfaces()` – scravy Apr 03 '15 at 22:54
4

If you're just looking for a unique server id, you could take the mongodb/bson approach and use the first n bytes of the md5 hash of the server's host name:

var machineHash = crypto.createHash('md5').update(os.hostname()).digest('binary');

This code is from node-buffalo. Not perfect, but may be good enough depending on what you're trying to do.

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
2

I tried the getmac package but it would not work for me (node v0.10, Mac OS X) - so I set out and built my own: https://github.com/scravy/node-macaddress . It works in Windows, OS X, Linux, and probably every unix with ifconfig :-)

scravy
  • 11,904
  • 14
  • 72
  • 127
1

The complete answer is further down, but basically you can get that info in vanilla node.js via:

require('os').networkInterfaces()

This gives you all the info about networking devices on the system, including MAC addresses for each interface.

You can further narrow this down to just the MACS:

JSON.stringify(  require('os').networkInterfaces(),  null,  2).match(/"mac": ".*?"/g)

And further, to the pure MAC addresses:

JSON.stringify(  require('os').networkInterfaces(),  null,  2).match(/"mac": ".*?"/g).toString().match(/\w\w:\w\w:\w\w:\w\w:\w\w:\w\w/g)

This last one will give you an array-like match object of the form:

['00:00:00:00:00:00', 'A8:AE:B6:58:C5:09', 'FC:E3:5A:42:80:18' ]

The first element is your lo or local interface.

I randomly generated the others for public example using https://www.miniwebtool.com/mac-address-generator/

If you want to be more 'proper' (or break it down into easier to digest steps):

var os = require('os');

var macs = ()=>{
  return JSON.stringify(  os.networkInterfaces(),  null,  2)
    .match(/"mac": ".*?"/g)
    .toString()
    .match(/\w\w:\w\w:\w\w:\w\w:\w\w:\w\w/g)
  ;
}


console.log( macs() );

Basically, you're taking the interface data object and converting it into a JSON text string. Then you get a match object for the mac addresses and convert that into a text string. Then you extract only the MAC addresses into an iteratable match object that contains only each MAC in each element.

There are surly more succinct ways of doing this, but this one is reliable and easy to read.

jdmayfield
  • 1,400
  • 1
  • 14
  • 26
0

Here is node.js code using command line tools to get MAC address:

`$ ifconfig | grep eth0 | awk '{print $5}'`

for wlan0

`$ ifconfig | grep wlan0 | awk '{print $5}'`

In node.js, use this - save the code to getMAC.js - "$ node getMAC.js" to run

`// Get MAC address - $ ifconfig | grep wlan0 | awk '{print $5}'
 var exec = require('child_process').exec;
 function puts(error, stdout, stderr) { console.log(stdout) }
 exec("ifconfig | grep wlan0 | awk '{print $5}'", puts);
 `
JamesNW
  • 127
  • 1
  • 2
  • 9