{ "nodes": [{ "uri": "https:\/\/log.exos.ninja\/42", "content": "# Capturar la MAC ADDRESS en node.js\r\n\r\nPor si se necesita para crear *namespaces* de uuid v1\/v2\/v3\/v5 por ejemplo.\r\n\r\n \/**\r\n * Get a mac address to generate unique space\r\n * @return {Buffer}\r\n *\/\r\n const getMac = (function() {\r\n let mac;\r\n let ifaces = os.networkInterfaces();\r\n\r\n for (const k in ifaces) {\r\n if (k.match(\/^[$_]\/)) {\r\n continue;\r\n }\r\n\r\n const ips = ifaces[k];\r\n\r\n if (!ips.length) {\r\n debug(`Iface ${k} has not ips`);\r\n continue;\r\n }\r\n\r\n const ipv4 = ips.find((x) => x.family === 'IPv4');\r\n\r\n if (ipv4 && !ipv4.internal) {\r\n debug(`Using ${k} mac address to generate uuids`);\r\n mac = ipv4.mac;\r\n break;\r\n }\r\n }\r\n\r\n if (!mac) {\r\n throw new Error(`No iface found; A network interface is needed to ` +\r\n `generate uuids`);\r\n }\r\n\r\n const bmac = new Buffer(mac.replace(\/:\/g, ''), 'hex'); \/\/ 6 bytes\r\n\r\n mac = null;\r\n ifaces = null;\r\n\r\n debug(`bmac is ${bmac.toString('hex')}`);\r\n\r\n return function() {\r\n return bmac;\r\n };\r\n\r\n })();", "created": "2019-11-21 18:05:13"}] }