pax_global_header00006660000000000000000000000064125171514710014516gustar00rootroot0000000000000052 comment=fac7dd3da9b3229b86655fd3e183e40dac48f01b node-macaddress-0.2.8/000077500000000000000000000000001251715147100145565ustar00rootroot00000000000000node-macaddress-0.2.8/.gitignore000066400000000000000000000000661251715147100165500ustar00rootroot00000000000000.DS_Store /node_modules .*.swp /.npmcache /dist *.log node-macaddress-0.2.8/.travis.yml000066400000000000000000000002151251715147100166650ustar00rootroot00000000000000language: node_js node_js: - stable - "0.12" - "0.11" - "0.10" - "0.9" - "0.8" - iojs - iojs-v1.0.4 install: - npm install node-macaddress-0.2.8/README.md000066400000000000000000000053511251715147100160410ustar00rootroot00000000000000node-macaddress =============== [![Build Status](https://travis-ci.org/scravy/node-macaddress.svg?branch=master)](https://travis-ci.org/scravy/node-macaddress) Retrieve MAC addresses in Linux, OS X, and Windows. A common misconception about MAC addresses is that every *host* had *one* MAC address, while a host may have *multiple* MAC addresses – since *every network interface* may have its own MAC address. This library allows to discover the MAC address per network interface and chooses an appropriate interface if all you're interested in is *one* MAC address identifying the host system (see `API + Examples` below). **Features:** + works on `Linux`, `Mac OS X`, `Windows`, and on most `UNIX` systems. + `node ≥ 0.12` and `io.js` report MAC addresses in `os.networkInterfaces()` this library utilizes this information when available. + also features a sane replacement for `os.networkInterfaces()` (see `API + Examples` below). + works with stoneage node versions ≥ v0.8 (...) Usage ----- ``` npm install --save macaddress ``` ```JavaScript var macaddress = require('macaddress'); ``` API + Examples -------------- (async) .one(iface, callback) → string (async) .one(callback) → string (async) .all(callback) → { iface: { type: address } } (sync) .networkInterfaces() → { iface: { type: address } } --- ### `.one([iface], callback)` Retrieves the MAC address of the given `iface`. If `iface` is omitted, this function automatically chooses an appropriate device (e.g. `eth0` in Linux, `en0` in OS X, etc.). **Without `iface` parameter:** ```JavaScript macaddress.one(function (err, mac) { console.log("Mac address for this host: %s", mac); }); ``` ``` → Mac address for this host: ab:42:de:13:ef:37 ``` **With `iface` parameter:** ```JavaScript macaddress.one('awdl0', function (err, mac) { console.log("Mac address for awdl0: %s", mac); }); ``` ``` → Mac address for awdl0: ab:cd:ef:34:12:56 ``` --- ### `.all(callback)` Retrieves the MAC addresses for all non-internal interfaces. ```JavaScript macaddress.all(function (err, all) { console.log(JSON.stringify(all, null, 2)); }); ``` ```JavaScript { "en0": { "ipv6": "fe80::cae0:ebff:fe14:1da9", "ipv4": "192.168.178.20", "mac": "ab:42:de:13:ef:37" }, "awdl0": { "ipv6": "fe80::58b9:daff:fea9:23a9", "mac": "ab:cd:ef:34:12:56" } } ``` --- ### `.networkInterfaces()` A useful replacement of `os.networkInterfaces()`. Reports only non-internal interfaces. ```JavaScript console.log(JSON.stringify(macaddress.networkInterfaces(), null, 2)); ``` ```JavaScript { "en0": { "ipv6": "fe80::cae0:ebff:fe14:1dab", "ipv4": "192.168.178.22" }, "awdl0": { "ipv6": "fe80::58b9:daff:fea9:23a9" } } ``` node-macaddress-0.2.8/gulpfile.js000066400000000000000000000004401251715147100167210ustar00rootroot00000000000000// vim: set et sw=2 ts=2 var gulp = require('gulp'); var jshint = require('gulp-jshint'); gulp.task('lint', function (done) { gulp.src([ "*.js", "lib/*.js" ]) .pipe(jshint()) .pipe(jshint.reporter('default')) .on('end', done); }); gulp.task('default', [ 'lint' ]); node-macaddress-0.2.8/index.js000066400000000000000000000066501251715147100162320ustar00rootroot00000000000000var os = require('os'); var lib = {}; function parallel(tasks, done) { var results = []; var errs = []; var length = 0; var doneLength = 0; function doneIt(ix, err, result) { if (err) { errs[ix] = err; } else { results[ix] = result; } doneLength += 1; if (doneLength >= length) { done(errs.length > 0 ? errs : errs, results); } } Object.keys(tasks).forEach(function (key) { length += 1; var task = tasks[key]; (global.setImmediate || global.setTimeout)(function () { task(doneIt.bind(null, key), 1); }); }); } lib.networkInterfaces = function () { var ifaces = os.networkInterfaces(); var allAddresses = {}; Object.keys(ifaces).forEach(function (iface) { addresses = {}; var hasAddresses = false; ifaces[iface].forEach(function (address) { if (!address.internal) { addresses[(address.family || "").toLowerCase()] = address.address; hasAddresses = true; if (address.mac) { addresses.mac = address.mac; } } }); if (hasAddresses) { allAddresses[iface] = addresses; } }); return allAddresses; }; var _getMacAddress; switch (os.platform()) { case 'win32': _getMacAddress = require('./lib/windows.js'); break; case 'linux': _getMacAddress = require('./lib/linux.js'); break; case 'darwin': case 'sunos': _getMacAddress = require('./lib/unix.js'); break; default: console.warn("node-macaddress: Unkown os.platform(), defaulting to `unix'."); _getMacAddress = require('./lib/unix.js'); break; } lib.one = function (iface, callback) { if (typeof iface === 'function') { callback = iface; var ifaces = lib.networkInterfaces(); var alleged = [ 'eth0', 'eth1', 'en0', 'en1' ]; iface = Object.keys(ifaces)[0]; for (var i = 0; i < alleged.length; i++) { if (ifaces[alleged[i]]) { iface = alleged[i]; break; } } if (!ifaces[iface]) { if (typeof callback === 'function') { callback("no interfaces found", null); } return null; } if (ifaces[iface].mac) { if (typeof callback === 'function') { callback(null, ifaces[iface].mac); } return ifaces[iface].mac; } } if (typeof callback === 'function') { _getMacAddress(iface, callback); } return null; }; lib.all = function (callback) { var ifaces = lib.networkInterfaces(); var resolve = {}; Object.keys(ifaces).forEach(function (iface) { if (!ifaces[iface].mac) { resolve[iface] = _getMacAddress.bind(null, iface); } }); if (Object.keys(resolve).length === 0) { if (typeof callback === 'function') { callback(null, ifaces); } return ifaces; } parallel(resolve, function (err, result) { Object.keys(result).forEach(function (iface) { ifaces[iface].mac = result[iface]; }); if (typeof callback === 'function') { callback(null, ifaces); } }); return null; }; module.exports = lib; node-macaddress-0.2.8/lib/000077500000000000000000000000001251715147100153245ustar00rootroot00000000000000node-macaddress-0.2.8/lib/linux.js000066400000000000000000000004631251715147100170240ustar00rootroot00000000000000var exec = require('child_process').exec; module.exports = function (iface, callback) { exec("cat /sys/class/net/" + iface + "/address", function (err, out) { if (err) { callback(err, null); return; } callback(null, out.trim().toLowerCase()); }); }; node-macaddress-0.2.8/lib/macosx.js000066400000000000000000000007311251715147100171550ustar00rootroot00000000000000var exec = require('child_process').exec; module.exports = function (iface, callback) { exec("networksetup -getmacaddress " + iface, function (err, out) { if (err) { callback(err, null); return; } var match = /[a-f0-9]{2}(:[a-f0-9]{2}){5}/.exec(out.toLowerCase()); if (!match) { callback("did not find a mac address", null); return; } callback(null, match[0]); }); }; node-macaddress-0.2.8/lib/unix.js000066400000000000000000000007241251715147100166500ustar00rootroot00000000000000var exec = require('child_process').exec; module.exports = function (iface, callback) { exec("ifconfig " + iface, function (err, out) { if (err) { callback(err, null); return; } var match = /[a-f0-9]{2}(:[a-f0-9]{2}){5}/.exec(out.toLowerCase()); if (!match) { callback("did not find a mac address", null); return; } callback(null, match[0].toLowerCase()); }); }; node-macaddress-0.2.8/lib/windows.js000066400000000000000000000015001251715147100173500ustar00rootroot00000000000000var exec = require('child_process').exec; var regexRegex = /[-\/\\^$*+?.()|[\]{}]/g; function escape(string) { return string.replace(regexRegex, '\\$&'); } module.exports = function (iface, callback) { exec("ipconfig /all", function (err, out) { if (err) { callback(err, null); return; } var match = new RegExp(escape(iface)).exec(out); if (!match) { callback("did not find interface in `ipconfig /all`", null); return; } out = out.substring(match.index + iface.length); match = /[A-Fa-f0-9]{2}(\-[A-Fa-f0-9]{2}){5}/.exec(out); if (!match) { callback("did not find a mac address", null); return; } callback(null, match[0].toLowerCase().replace(/\-/g, ':')); }); }; node-macaddress-0.2.8/package.json000066400000000000000000000011401251715147100170400ustar00rootroot00000000000000{ "name": "macaddress", "version": "0.2.8", "description": "Get the MAC addresses (hardware addresses) of the hosts network interfaces.", "main": "index.js", "scripts": { "test": "node test.js" }, "repository": { "type": "git", "url": "https://github.com/scravy/node-macaddress.git" }, "keywords": [ "mac", "mac-address", "hardware-address", "network", "system" ], "author": "Julian Fleischer", "license": "MIT", "bugs": { "url": "https://github.com/scravy/node-macaddress/issues" }, "homepage": "https://github.com/scravy/node-macaddress" } node-macaddress-0.2.8/test.js000066400000000000000000000007401251715147100160740ustar00rootroot00000000000000var macaddress = require('./index'); var sync = macaddress.one(function (err, mac) { if (err || !/[a-f0-9]{2}(:[a-f0-9]{2}){5}/.test(mac)) { throw err || mac; } console.log("Mac address for this host: %s", mac); }); console.log("Mac address obtained synchronously: %s", sync); macaddress.all(function (err, all) { if (err) { throw err; } console.log(JSON.stringify(all, null, 2)); }); console.log(JSON.stringify(macaddress.networkInterfaces(), null, 2));