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.
Using the uuid Package Unlike the crypto module, the uuid package is a third-party npm module. To install it, run the following command. uuid allows you to generate different ID versions: Version 1 and 4 generate a unique ID randomly generated.
There is no way to get any kind of identifier that is truly unique and unchangeable from the client. This means no MAC address, serial number, IMSI, or any of those other things.
UUIDs are typically used as unique identifiers. You can also use them in JavaScript and Node. js. The Node. js team recently added native support to generate a UUID to Node.
To uniquely identify the destination of an IP packet arriving over the network, you have to extend the port principle with information about the protocol used and the IP address of the network interface; this information is called a socket. A socket has three parts: protocol, local-address, local-port.
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.
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.
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