Skip to content

Instantly share code, notes, and snippets.

@fkei
Created March 4, 2011 04:59
Show Gist options
  • Save fkei/854196 to your computer and use it in GitHub Desktop.
Save fkei/854196 to your computer and use it in GitHub Desktop.
Mac OSX and Linux support.
#!/usr/bin/env node
var os = require('os');
var osType = os.type();
var search = function() {};
if (osType === 'Darwin') {
var nic = 'en0'; // Mac OSX
search = function(result) {
for (var i = 0; i < res.length; i++) {
var d = res[i].split('\n');
for (var j = 0; j < d.length; j++) {
var l = d[j].replace(/(\t)+/g, '');
var ans = l.match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/); // 先頭のみ
if (ans) {
console.log(ans[0]);
}
}
}
};
} else if (osType === 'Linux') {
var nic = 'eth0'; // Linux
search = function(result) {
for (var i = 0; i < res.length; i++) {
var d = res[i].split('\n');
for (var j = 0; j < d.length; j++) {
var l = d[j].replace(/(\t)+/g, '');
var ans = l.match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/); // 先頭のみ
if (ans) {
console.log(ans[0]);
}
}
}
};
} else {
console.log('not support os.');
process.exit(1);
}
var spawn = require('child_process').spawn;
var ifconfig = spawn('/sbin/ifconfig', [nic]);
var res = new Array();
ifconfig.stdout.setEncoding('utf8');
ifconfig.stdout.addListener('data', function(result) {
//res.push(result.replace(/(\n|\t)+/g, ''));
res.push(result);
});
ifconfig.stderr.setEncoding('utf8');
ifconfig.stderr.addListener('data', function(result) {
//res.push(result.replace(/(\n|\t)+/g, ''));
res.push(result);
});
ifconfig.addListener('exit', function(result) {
search(result);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment