Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #11 from sevir/master

fix getnetworkip in linux
  • Loading branch information...
commit 5d13cf9db834cdc25314cdb49696b4e1688ae3c7 2 parents eb2e76d + 7f1c9f9
@krawaller authored
Showing with 77 additions and 38 deletions.
  1. +77 −38 lib/getnetworkip.js
View
115 lib/getnetworkip.js
@@ -1,53 +1,92 @@
module.exports = (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) {
- // TODO: implement for OSs without ifconfig command
- 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;
- }
+ var cached;
+ var os=require('os');
- return function (callback, bypassCache) {
- // get cached value
- if (cached && !bypassCache) {
- callback(null, cached);
- return;
- }
- // system call
- exec(command, function (error, stdout, sterr) {
+ if(os.networkInterfaces || os.getNetworkInterfaces){
+ //getNetworkInterfaces for node v0.5.x deprecated in v0.6.x
+ var networks = os.networkInterfaces? os.networkInterfaces() : os.getNetworkInterfaces();
+
+ return function (callback, bypassCache){
+ // get cached value
+ if (cached && !bypassCache) {
+ callback(null, cached);
+ return;
+ }
+
+ var ifaces=os.networkInterfaces();
var ips = [];
- // extract IPs
- var matches = stdout.match(filterRE);
- // JS has no lookbehind REs, so we need a trick
- for (var i = 0; i < matches.length; i++) {
- ips.push(matches[i].replace(filterRE, '$1'));
+ for (var dev in ifaces) {
+ ifaces[dev].forEach(function(details){
+ if (details.family=='IPv4') {
+ ips.push(details.address);
+ }
+ });
}
- // filter BS
+ // filter BS
for (var i = 0, l = ips.length; i < l; i++) {
if (!ignoreRE.test(ips[i])) {
- //if (!error) {
- cached = ips[i];
- //}
- callback(error, ips[i]);
+ cached = ips[i];
+ callback(null, ips[i]);
return;
}
}
// nothing found
callback(error, null);
- });
- };
+ };
+ }else{
+ //Deprecated, only for supporting node v0.4.x
+ var exec = require('child_process').exec;
+ var command;
+ var filterRE;
+
+ switch (process.platform) {
+ // TODO: implement for OSs without ifconfig command
+ case 'darwin':
+ command = 'ifconfig';
+ filterRE = /\binet\s+([^\s]+)/g;
+ // filterRE = /\binet6\s+([^\s]+)/g; // IPv6
+ break;
+ default:
+ command = 'ifconfig';
+ filterRE = /\binet:\s*([^\s]+)/g;
+ // filterRE = /\binet6[^:]+:\s*([^\s]+)/g; // IPv6
+ break;
+ }
+
+ return function (callback, bypassCache) {
+ // get cached value
+ if (cached && !bypassCache) {
+ callback(null, cached);
+ return;
+ }
+
+
+ // system call
+ exec(command, function (error, stdout, sterr) {
+ var ips = [];
+ // extract IPs
+ var matches = stdout.match(filterRE);
+ // JS has no lookbehind REs, so we need a trick
+ for (var i = 0; i < matches.length; i++) {
+ ips.push(matches[i].replace(filterRE, '$1'));
+ }
+
+ // filter BS
+ for (var i = 0, l = ips.length; i < l; i++) {
+ if (!ignoreRE.test(ips[i])) {
+ //if (!error) {
+ cached = ips[i];
+ //}
+ callback(error, ips[i]);
+ return;
+ }
+ }
+ // nothing found
+ callback(error, null);
+ });
+ };
+ }
})();
Please sign in to comment.
Something went wrong with that request. Please try again.