Skip to content

Instantly share code, notes, and snippets.

@kumarasinghe
Last active October 27, 2020 12:44
Show Gist options
  • Save kumarasinghe/ec4335082896fa333bd6f6bc4c77def1 to your computer and use it in GitHub Desktop.
Save kumarasinghe/ec4335082896fa333bd6f6bc4c77def1 to your computer and use it in GitHub Desktop.
Get a MAC address unique to the device in Node.js
getMACAddress() {
let vmMACPrefixes = [
"00:50:56",
"00:0C:29",
"00:05:69",
"00:1C:14",
"0A:00:27",
"00:03:FF",
"00:1C:42",
"00:0F:4B",
"00:16:3E",
"08:00:27",
"02:42",
];
let networkInterfaces = require('os').networkInterfaces();
for (let interfaceName in networkInterfaces) {
let interfaceDetails = networkInterfaces[interfaceName][0];
if (
// interface is not internal
!interfaceDetails.internal &&
// interface is not from a virtual machine
vmMACPrefixes.reduce(
(accumulator, currentValue) =>
interfaceDetails.mac.startsWith(currentValue) ? false : accumulator,
true
)
) {
return interfaceDetails.mac;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment