├── .editorconfig ├── .github └── workflows │ └── npmpublish.yml ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.d.ts ├── index.js ├── lib ├── getallinterfaces.js ├── getmacaddress.js ├── networkinterfaces.js ├── platform │ ├── getallinterfaces_linux.js │ ├── getallinterfaces_unix.js │ ├── getallinterfaces_windows.js │ ├── getmacaddress_linux.js │ ├── getmacaddress_unix.js │ └── getmacaddress_windows.js └── util.js ├── package-lock.json ├── package.json └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | 6 | [*.js] 7 | indent_style = space 8 | indent_size = 4 9 | -------------------------------------------------------------------------------- /.github/workflows/npmpublish.yml: -------------------------------------------------------------------------------- 1 | # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages 3 | 4 | name: Node.js Package 5 | 6 | on: 7 | release: 8 | types: [created] 9 | 10 | jobs: 11 | build: 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v2 15 | - uses: actions/setup-node@v1 16 | with: 17 | node-version: 12 18 | - run: npm ci 19 | - run: npm test 20 | 21 | publish-npm: 22 | needs: build 23 | runs-on: ubuntu-latest 24 | steps: 25 | - uses: actions/checkout@v2 26 | - uses: actions/setup-node@v1 27 | with: 28 | node-version: 12 29 | registry-url: https://registry.npmjs.org/ 30 | - run: npm ci 31 | - run: npm publish 32 | env: 33 | NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}} 34 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .* 2 | !/.gitignore 3 | !/.editorconfig 4 | !/.travis.yml 5 | !/.github 6 | /node_modules 7 | /dist 8 | *.log 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | os: 3 | - linux 4 | - osx 5 | - windows 6 | node_js: 7 | - stable 8 | - lts/* 9 | - "10.5" 10 | - "8.11" 11 | - "6.14" 12 | - "4.9" 13 | - "0.12" 14 | - "0.11" 15 | - "0.10" 16 | - "0.9" 17 | - "0.8" 18 | - iojs 19 | - iojs-v1.0.4 20 | install: 21 | - npm install 22 | jobs: 23 | exclude: 24 | - os: windows 25 | node_js: stable 26 | - os: windows 27 | node_js: iojs 28 | - os: windows 29 | node_js: iojs-v1.0.4 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Julian Fleischer 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | node-macaddress 2 | =============== 3 | 4 | [![Build Status](https://travis-ci.org/scravy/node-macaddress.svg?branch=master)](https://travis-ci.org/scravy/node-macaddress) 5 | 6 | Retrieve [MAC addresses](https://en.wikipedia.org/wiki/MAC_address) in Linux, OS X, and Windows. 7 | 8 | A common misconception about MAC addresses is that every *host* had *one* MAC address, 9 | while a host may have *multiple* MAC addresses – since *every network interface* may 10 | have its own MAC address. 11 | 12 | This library allows to discover the MAC address per network interface and chooses 13 | an appropriate interface if all you're interested in is *one* MAC address identifying 14 | the host system (see `API + Examples` below). 15 | 16 | A common misconception about this library is that it reports the mac address of the client that accesses some kind of backend. It does not. Reporting the mac address is not in any way similar to reporting the IP address of the client that accesses your application. This library reports the mac address of the server the application is running on. This useful for example for distributed/scaled applications, for example when generating UUIDs. 17 | 18 | Also it seems to be worth noting that this library is not intended to be used in a browser. There is no web api reporting the mac address of the machine (and that is a good thing). 19 | 20 | **Features:** 21 | 22 | + works on `Linux`, `Mac OS X`, `Windows`, and on most `UNIX` systems. 23 | + `node ≥ 0.12` and `io.js` report MAC addresses in `os.networkInterfaces()` 24 | this library utilizes this information when available. 25 | + also features a sane replacement for `os.networkInterfaces()` 26 | (see `API + Examples` below). 27 | + works with stoneage node versions ≥ v0.8 (...) 28 | + Promise support 29 | 30 | Usage 31 | ----- 32 | 33 | ``` 34 | npm install --save macaddress 35 | ``` 36 | 37 | ```JavaScript 38 | var macaddress = require('macaddress'); 39 | ``` 40 | 41 | API + Examples 42 | -------------- 43 | 44 | (async) .one(iface, callback) → string 45 | (async) .one(iface) → Promise 46 | (async) .one(callback) → string 47 | (async) .all() → Promise<{ iface: { type: address } }> 48 | (async) .all(callback) → { iface: { type: address } } 49 | (sync) .networkInterfaces() → { iface: { type: address } } 50 | 51 | --- 52 | 53 | ### `.one([iface], callback)` 54 | 55 | Retrieves the MAC address of the given `iface`. 56 | 57 | If `iface` is omitted, this function automatically chooses an 58 | appropriate device (e.g. `eth0` in Linux, `en0` in OS X, etc.). 59 | 60 | **Without `iface` parameter:** 61 | 62 | ```JavaScript 63 | macaddress.one(function (err, mac) { 64 | console.log("Mac address for this host: %s", mac); 65 | }); 66 | ``` 67 | 68 | or using Promise 69 | 70 | ```JavaScript 71 | macaddress.one().then(function (mac) { 72 | console.log("Mac address for this host: %s", mac); 73 | }); 74 | ``` 75 | 76 | ``` 77 | → Mac address for this host: ab:42:de:13:ef:37 78 | ``` 79 | 80 | **With `iface` parameter:** 81 | 82 | ```JavaScript 83 | macaddress.one('awdl0', function (err, mac) { 84 | console.log("Mac address for awdl0: %s", mac); 85 | }); 86 | ``` 87 | or using Promise 88 | 89 | ```JavaScript 90 | macaddress.one('awdl0').then(function (mac) { 91 | console.log("Mac address for awdl0: %s", mac); 92 | }); 93 | ``` 94 | 95 | ``` 96 | → Mac address for awdl0: ab:cd:ef:34:12:56 97 | ``` 98 | 99 | --- 100 | 101 | ### `.all(callback)` 102 | 103 | Retrieves the MAC addresses for all non-internal interfaces. 104 | 105 | ```JavaScript 106 | macaddress.all(function (err, all) { 107 | console.log(JSON.stringify(all, null, 2)); 108 | }); 109 | ``` 110 | or using Promise 111 | 112 | ```JavaScript 113 | macaddress.all().then(function (all) { 114 | console.log(JSON.stringify(all, null, 2)); 115 | }); 116 | ``` 117 | 118 | ```JavaScript 119 | { 120 | "en0": { 121 | "ipv6": "fe80::cae0:ebff:fe14:1da9", 122 | "ipv4": "192.168.178.20", 123 | "mac": "ab:42:de:13:ef:37" 124 | }, 125 | "awdl0": { 126 | "ipv6": "fe80::58b9:daff:fea9:23a9", 127 | "mac": "ab:cd:ef:34:12:56" 128 | } 129 | } 130 | ``` 131 | 132 | --- 133 | 134 | ### `.networkInterfaces()` 135 | 136 | A useful replacement of `os.networkInterfaces()`. Reports only non-internal interfaces. 137 | 138 | ```JavaScript 139 | console.log(JSON.stringify(macaddress.networkInterfaces(), null, 2)); 140 | ``` 141 | 142 | ```JavaScript 143 | { 144 | "en0": { 145 | "ipv6": "fe80::cae0:ebff:fe14:1dab", 146 | "ipv4": "192.168.178.22" 147 | }, 148 | "awdl0": { 149 | "ipv6": "fe80::58b9:daff:fea9:23a9" 150 | } 151 | } 152 | ``` 153 | 154 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | declare module 'macaddress' { 2 | export type MacAddresCallback = (err: any, data: any) => void; 3 | 4 | export type MacAddressOneCallback = (err: any, mac: string) => void; 5 | 6 | export function one(callback: MacAddressOneCallback): void; 7 | export function one(iface?: string): Promise; 8 | export function one(iface: string, callback: MacAddressOneCallback): void; 9 | 10 | export function all(callback: MacAddresCallback): void; 11 | export function all(): Promise; 12 | 13 | export function networkInterfaces(): any; 14 | } 15 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* jshint node: true */ 2 | "use strict"; 3 | 4 | var util = require("./lib/util.js"); 5 | var lib = {}; 6 | 7 | lib.getMacAddress = require("./lib/getmacaddress.js"); 8 | lib.getAllInterfaces = require("./lib/getallinterfaces.js"); 9 | lib.networkInterfaces = require("./lib/networkinterfaces.js"); 10 | 11 | // devices like en0 (mac), eth3 (linux), Ethernet (windows), etc. are preferred 12 | var goodIfaces = new RegExp("^((en|eth)[0-9]+|ethernet)$", "i"); 13 | 14 | // https://github.com/scravy/node-macaddress/issues/32 15 | var badIfaces = new RegExp("^(vboxnet[0-9]+)$", "i"); 16 | 17 | lib.one = function () { 18 | // one() can be invoked in several ways: 19 | // one() -> Promise 20 | // one(iface: string) -> Promise 21 | // one(iface: string, callback) -> async, yields a string 22 | // one(callback) -> async, yields a string 23 | var iface = null; 24 | var callback = null; 25 | if (arguments.length >= 1) { 26 | if (typeof arguments[0] === "function") { 27 | callback = arguments[0]; 28 | } else if (typeof arguments[0] === "string") { 29 | iface = arguments[0]; 30 | } 31 | if (arguments.length >= 2) { 32 | if (typeof arguments[1] === "function") { 33 | callback = arguments[1]; 34 | } 35 | } 36 | } 37 | if (!callback) { 38 | return util.promisify(function (callback) { 39 | lib.one(iface, callback); 40 | }); 41 | } 42 | if (iface) { 43 | lib.getMacAddress(iface, callback); 44 | return; 45 | } 46 | var ifaces = lib.networkInterfaces(); 47 | var addresses = {}; 48 | var best = []; 49 | var args = []; 50 | Object.keys(ifaces).forEach(function (name) { 51 | args.push(name); 52 | var score = 0; 53 | var iface = ifaces[name]; 54 | if (typeof iface.mac === "string" && iface.mac !== "00:00:00:00:00:00") { 55 | addresses[name] = iface.mac; 56 | if (iface.ipv4) { 57 | score += 1; 58 | } 59 | if (iface.ipv6) { 60 | score += 1; 61 | } 62 | if (goodIfaces.test(name)) { 63 | score += 2; 64 | } 65 | if (badIfaces.test(name)) { 66 | score -= 3; 67 | } 68 | best.push({ 69 | name: name, 70 | score: score, 71 | mac: iface.mac 72 | }); 73 | } 74 | }); 75 | if (best.length > 0) { 76 | best.sort(function (left, right) { 77 | // the following will sort items with a higher score to the beginning 78 | var comparison = right.score - left.score; 79 | if (comparison !== 0) { 80 | return comparison; 81 | } 82 | if (left.name < right.name) { 83 | return -1; 84 | } 85 | if (left.name > right.name) { 86 | return 1; 87 | } 88 | return 0; 89 | }); 90 | util.nextTick(callback.bind(null, null, best[0].mac)); 91 | return; 92 | } 93 | args.push(lib.getAllInterfaces); 94 | var getMacAddress = function (d, cb) { 95 | if (addresses[d]) { 96 | cb(null, addresses[d]); 97 | return; 98 | } 99 | lib.getMacAddress(d, cb); 100 | }; 101 | util.iterate(args, getMacAddress, callback); 102 | }; 103 | 104 | lib.all = function (callback) { 105 | if (typeof callback !== "function") { 106 | return util.promisify(lib.all); 107 | } 108 | var ifaces = lib.networkInterfaces(); 109 | var resolve = {}; 110 | Object.keys(ifaces).forEach(function (iface) { 111 | if (!ifaces[iface].mac) { 112 | resolve[iface] = lib.getMacAddress.bind(null, iface); 113 | } 114 | }); 115 | if (Object.keys(resolve).length === 0) { 116 | if (typeof callback === "function") { 117 | util.nextTick(callback.bind(null, null, ifaces)); 118 | } 119 | return ifaces; 120 | } 121 | util.parallel(resolve, function (err, result) { 122 | Object.keys(result).forEach(function (iface) { 123 | ifaces[iface].mac = result[iface]; 124 | }); 125 | if (typeof callback === "function") { 126 | callback(null, ifaces); 127 | } 128 | }); 129 | return null; 130 | }; 131 | 132 | module.exports = lib; 133 | -------------------------------------------------------------------------------- /lib/getallinterfaces.js: -------------------------------------------------------------------------------- 1 | var os = require('os'); 2 | 3 | var _getAllInterfaces; 4 | switch (os.platform()) { 5 | 6 | case 'win32': 7 | _getAllInterfaces = require('./platform/getallinterfaces_windows.js'); 8 | break; 9 | 10 | case 'linux': 11 | _getAllInterfaces = require('./platform/getallinterfaces_linux.js'); 12 | break; 13 | 14 | case 'darwin': 15 | case 'sunos': 16 | case 'freebsd': 17 | _getAllInterfaces = require('./platform/getallinterfaces_unix.js'); 18 | break; 19 | 20 | default: 21 | console.warn("node-macaddress: Unknown os.platform(), defaulting to 'unix'."); 22 | _getAllInterfaces = require('./platform/getallinterfaces_unix.js'); 23 | break; 24 | 25 | } 26 | 27 | module.exports = _getAllInterfaces; 28 | 29 | -------------------------------------------------------------------------------- /lib/getmacaddress.js: -------------------------------------------------------------------------------- 1 | var os = require('os'); 2 | 3 | var _getMacAddress; 4 | var _validIfaceRegExp = '^[a-z0-9]+$'; 5 | switch (os.platform()) { 6 | 7 | case 'win32': 8 | // windows has long interface names which may contain spaces and dashes 9 | _validIfaceRegExp = '^[a-z0-9 -]+$'; 10 | _getMacAddress = require('./platform/getmacaddress_windows.js'); 11 | break; 12 | 13 | case 'linux': 14 | _getMacAddress = require('./platform/getmacaddress_linux.js'); 15 | break; 16 | 17 | case 'darwin': 18 | case 'sunos': 19 | case 'freebsd': 20 | _getMacAddress = require('./platform/getmacaddress_unix.js'); 21 | break; 22 | 23 | default: 24 | console.warn("node-macaddress: Unknown os.platform(), defaulting to 'unix'."); 25 | _getMacAddress = require('./platform/getmacaddress_unix.js'); 26 | break; 27 | 28 | } 29 | 30 | var validIfaceRegExp = new RegExp(_validIfaceRegExp, 'i'); 31 | 32 | module.exports = function (iface, callback) { 33 | 34 | // some platform specific ways of resolving the mac address pass the name 35 | // of the interface down to some command processor, so check for a well 36 | // formed string here. 37 | if (!validIfaceRegExp.test(iface)) { 38 | callback(new Error([ 39 | 'invalid iface: \'', iface, 40 | '\' (must conform to reg exp /', 41 | validIfaceRegExp, '/)' 42 | ].join('')), null); 43 | return; 44 | } 45 | 46 | _getMacAddress(iface, callback); 47 | } 48 | 49 | -------------------------------------------------------------------------------- /lib/networkinterfaces.js: -------------------------------------------------------------------------------- 1 | var os = require('os'); 2 | 3 | // Retrieves all interfaces that do feature some non-internal address. 4 | // This function does NOT employ caching as to reflect the current state 5 | // of the machine accurately. 6 | module.exports = function () { 7 | var allAddresses = {}; 8 | 9 | try { 10 | var ifaces = os.networkInterfaces(); 11 | } catch (e) { 12 | // At October 2016 WSL does not support os.networkInterfaces() and throws 13 | // Return empty object as if no interfaces were found 14 | // https://github.com/Microsoft/BashOnWindows/issues/468 15 | if (e.syscall === 'uv_interface_addresses') { 16 | return allAddresses; 17 | } else { 18 | throw e; 19 | } 20 | } 21 | 22 | Object.keys(ifaces).forEach(function (iface) { 23 | var addresses = {}; 24 | var hasAddresses = false; 25 | ifaces[iface].forEach(function (address) { 26 | if (!address.internal) { 27 | var family = (typeof address.family === 'number') 28 | ? ("ipv" + address.family) 29 | : (address.family || "").toLowerCase(); 30 | addresses[family] = address.address; 31 | hasAddresses = true; 32 | if (address.mac && address.mac !== '00:00:00:00:00:00') { 33 | addresses.mac = address.mac; 34 | } 35 | } 36 | }); 37 | if (hasAddresses) { 38 | allAddresses[iface] = addresses; 39 | } 40 | }); 41 | return allAddresses; 42 | }; 43 | 44 | -------------------------------------------------------------------------------- /lib/platform/getallinterfaces_linux.js: -------------------------------------------------------------------------------- 1 | /* jshint node: true */ 2 | var execFile = require('child_process').execFile; 3 | 4 | module.exports = function (callback) { 5 | execFile("/bin/ls", ["/sys/class/net"], function (err, out) { 6 | if (err) { 7 | callback(err, null); 8 | return; 9 | } 10 | var ifaces = out.split(/[ \t\n]+/); 11 | var result = []; 12 | for (var i = 0; i < ifaces.length; i += 1) { 13 | var iface = ifaces[i].trim(); 14 | if (iface !== "") { 15 | result.push(iface); 16 | } 17 | } 18 | callback(null, result); 19 | }); 20 | }; 21 | -------------------------------------------------------------------------------- /lib/platform/getallinterfaces_unix.js: -------------------------------------------------------------------------------- 1 | /* jshint node: true */ 2 | 'use strict'; 3 | 4 | var execFile = require('child_process').execFile; 5 | 6 | module.exports = function (callback) { 7 | execFile("/sbin/ifconfig", ["-l"], function (err, out) { 8 | if (err) { 9 | callback(err, null); 10 | return; 11 | } 12 | var ifaces = out.split(/[ \t]+/); 13 | var result = []; 14 | for (var i = 0; i < ifaces.length; i += 1) { 15 | var iface = ifaces[i].trim(); 16 | if (iface !== "") { 17 | result.push(iface); 18 | } 19 | } 20 | callback(null, result); 21 | }); 22 | }; 23 | -------------------------------------------------------------------------------- /lib/platform/getallinterfaces_windows.js: -------------------------------------------------------------------------------- 1 | /* jshint node: true */ 2 | 'use strict'; 3 | 4 | var execFile = require('child_process').execFile; 5 | 6 | module.exports = function (callback) { 7 | execFile("wmic", ["nic", "get", "NetConnectionID"], function (err, out) { 8 | if (err) { 9 | callback(err, null); 10 | return; 11 | } 12 | var ifaces = out.trim().replace(/\s{2,}/g, "\n").split("\n").slice(1); 13 | var result = []; 14 | for (var i = 0; i < ifaces.length; i += 1) { 15 | var iface = ifaces[i].trim(); 16 | if (iface !== "") { 17 | result.push(iface); 18 | } 19 | } 20 | callback(null, result); 21 | }); 22 | }; 23 | -------------------------------------------------------------------------------- /lib/platform/getmacaddress_linux.js: -------------------------------------------------------------------------------- 1 | /* jshint node: true */ 2 | var execFile = require('child_process').execFile; 3 | 4 | module.exports = function (iface, callback) { 5 | execFile("/bin/cat", ["/sys/class/net/" + iface + "/address"], function (err, out) { 6 | if (err) { 7 | callback(err, null); 8 | return; 9 | } 10 | callback(null, out.trim().toLowerCase()); 11 | }); 12 | }; 13 | -------------------------------------------------------------------------------- /lib/platform/getmacaddress_unix.js: -------------------------------------------------------------------------------- 1 | /* jshint node: true */ 2 | var execFile = require('child_process').execFile; 3 | 4 | module.exports = function (iface, callback) { 5 | execFile("ifconfig", [iface], function (err, out) { 6 | if (err) { 7 | callback(err, null); 8 | return; 9 | } 10 | var match = /[a-f0-9]{2}(:[a-f0-9]{2}){5}/.exec(out.toLowerCase()); 11 | if (!match) { 12 | callback("did not find a mac address", null); 13 | return; 14 | } 15 | callback(null, match[0].toLowerCase()); 16 | }); 17 | }; 18 | -------------------------------------------------------------------------------- /lib/platform/getmacaddress_windows.js: -------------------------------------------------------------------------------- 1 | /* jshint node: true */ 2 | var execFile = require('child_process').execFile; 3 | 4 | var regexRegex = /[-\/\\^$*+?.()|[\]{}]/g; 5 | 6 | function escape(string) { 7 | return string.replace(regexRegex, '\\$&'); 8 | } 9 | 10 | module.exports = function (iface, callback) { 11 | execFile("ipconfig", ["/all"], function (err, out) { 12 | if (err) { 13 | callback(err, null); 14 | return; 15 | } 16 | var match = new RegExp(escape(iface)).exec(out); 17 | if (!match) { 18 | callback("did not find interface in `ipconfig /all`", null); 19 | return; 20 | } 21 | out = out.substring(match.index + iface.length); 22 | match = /[A-Fa-f0-9]{2}(\-[A-Fa-f0-9]{2}){5}/.exec(out); 23 | if (!match) { 24 | callback("did not find a mac address", null); 25 | return; 26 | } 27 | callback(null, match[0].toLowerCase().replace(/\-/g, ':')); 28 | }); 29 | }; 30 | -------------------------------------------------------------------------------- /lib/util.js: -------------------------------------------------------------------------------- 1 | /* jshint node: true */ 2 | "use strict"; 3 | 4 | var lib = {}; 5 | 6 | var nextTick = process.nextTick || global.setImmediate || global.setTimeout; 7 | lib.nextTick = function (func) { 8 | nextTick(func); 9 | }; 10 | 11 | lib.parallel = function (tasks, done) { 12 | var results = []; 13 | var errs = []; 14 | var length = 0; 15 | var doneLength = 0; 16 | function doneIt(ix, err, result) { 17 | if (err) { 18 | errs[ix] = err; 19 | } else { 20 | results[ix] = result; 21 | } 22 | doneLength += 1; 23 | if (doneLength >= length) { 24 | done(errs.length > 0 ? errs : errs, results); 25 | } 26 | } 27 | Object.keys(tasks).forEach(function (key) { 28 | length += 1; 29 | var task = tasks[key]; 30 | lib.nextTick(function () { 31 | task(doneIt.bind(null, key), 1); 32 | }); 33 | }); 34 | }; 35 | 36 | lib.promisify = function (func) { 37 | return new Promise(function (resolve, reject) { 38 | func(function (err, data) { 39 | if (err) { 40 | if (!err instanceof Error) { 41 | err = new Error(err); 42 | } 43 | reject(err); 44 | return; 45 | } 46 | resolve(data); 47 | }); 48 | }); 49 | }; 50 | 51 | lib.iterate = function (args, func, callback) { 52 | var errors = []; 53 | var f = function () { 54 | if (args.length === 0) { 55 | lib.nextTick(callback.bind(null, errors)); 56 | return; 57 | } 58 | var arg = args.shift(); 59 | if (typeof arg === "function") { 60 | arg(function (err, res) { 61 | if (err) { 62 | errors.push(err); 63 | } else { 64 | while (res.length > 0) { 65 | args.unshift(res.pop()); 66 | } 67 | } 68 | f(); 69 | }); 70 | return; 71 | } 72 | func(arg, function (err, res) { 73 | if (err) { 74 | errors.push(err); 75 | f(); 76 | } else { 77 | lib.nextTick(callback.bind(null, null, res)); 78 | } 79 | }); 80 | }; 81 | lib.nextTick(f); 82 | }; 83 | 84 | module.exports = lib; 85 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "macaddress", 3 | "version": "0.5.2", 4 | "lockfileVersion": 2, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "macaddress", 9 | "version": "0.5.2", 10 | "license": "MIT" 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "macaddress", 3 | "version": "0.5.2", 4 | "description": "Get the MAC addresses (hardware addresses) of the hosts network interfaces.", 5 | "main": "index.js", 6 | "typings": "index.d.ts", 7 | "scripts": { 8 | "test": "node test.js" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/scravy/node-macaddress.git" 13 | }, 14 | "keywords": [ 15 | "mac", 16 | "mac-address", 17 | "hardware-address", 18 | "network", 19 | "system" 20 | ], 21 | "author": "Julian Fleischer", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/scravy/node-macaddress/issues" 25 | }, 26 | "homepage": "https://github.com/scravy/node-macaddress" 27 | } 28 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | /* jshint node: true */ 2 | 'use strict'; 3 | 4 | var macaddress = require('./index.js'); 5 | 6 | macaddress.one(function (err, mac) { 7 | if (err || !/[a-f0-9]{2}(:[a-f0-9]{2}){5}/.test(mac)) { 8 | console.log(mac + " does not work"); 9 | throw err || mac; 10 | } 11 | console.log("Mac address for this host: %s", mac); 12 | }); 13 | 14 | macaddress.all(function (err, all) { 15 | if (err) { 16 | throw err; 17 | } 18 | console.log(JSON.stringify(all, null, 2)); 19 | }); 20 | 21 | console.log(JSON.stringify(macaddress.networkInterfaces(), null, 2)); 22 | 23 | if (typeof Promise !== 'undefined') { 24 | macaddress.one().then(function (result) { 25 | console.log("Mac address for this host using Promises: %s", result); 26 | }); 27 | macaddress.all().then(function (result) { 28 | console.log("all() using promises"); 29 | console.log(JSON.stringify(result, null, 2)); 30 | }); 31 | } 32 | --------------------------------------------------------------------------------