Last active
October 27, 2020 12:44
-
-
Save kumarasinghe/ec4335082896fa333bd6f6bc4c77def1 to your computer and use it in GitHub Desktop.
Get a MAC address unique to the device in Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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