forked from Reregistered/Node-NetworkIP
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dov Amihod
authored and
Dov Amihod
committed
May 3, 2012
0 parents
commit b4d27bf
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
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
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; |
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
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" } | ||
} |