Skip to content

Commit

Permalink
first version.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dov Amihod authored and Dov Amihod committed May 3, 2012
0 parents commit b4d27bf
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
54 changes: 54 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
*/

var getNetworkIPs = (function () {
var ignoreRE = /^(127\.0\.0\.1|::1|fe80(:1)?::1(%.*)?)$/i;

var exec = require('child_process').exec;
var cached;
var command;
var filterRE;

switch (process.platform) {
case 'win32':
//case 'win64': // TODO: test
command = 'ipconfig';
filterRE = /\bIP-[^:\r\n]+:\s*([^\s]+)/g;
// TODO: find IPv6 RegEx
break;
case 'darwin':
command = 'ifconfig';
filterRE = /\binet\s+([^\s]+)/g;
// filterRE = /\binet6\s+([^\s]+)/g; // IPv6
break;
default:
command = 'ifconfig';
filterRE = /\binet\b[^:]+:\s*([^\s]+)/g;
// filterRE = /\binet6[^:]+:\s*([^\s]+)/g; // IPv6
break;
}

return function (callback, bypassCache) {
if (cached && !bypassCache) {
callback(null, cached);
return;
}
// system call
exec(command, function (error, stdout, sterr) {
cached = [];
var ip;
var matches = stdout.match(filterRE) || [];
//if (!error) {
for (var i = 0; i < matches.length; i++) {
ip = matches[i].replace(filterRE, '$1')
if (!ignoreRE.test(ip)) {
cached.push(ip);
}
}
//}
callback(error, cached);
});
};
})();

exports.IPs = getNetworkIPs;
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "node-NetworkIP"
, "version": "0.0.1"
, "description": "Simple module to get IP of the platform"
, "keywords": ["Network", "IP"]
, "repository" :
{ "type" : "git"
, "url" : "http://github.com/reregistered/node-NetworkIP.git"
}
, "author": "Dov Amihod "
, "dependencies": {
}
, "main": "index"
, "engines": { "node": ">= 0.4.0 < 0.7.0" }
}

0 comments on commit b4d27bf

Please sign in to comment.