-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip.js
85 lines (74 loc) · 2.46 KB
/
ip.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var shell = require('child_process');
var sys = require('sys');
var nic = '';
if (process.argv.length === 3) {
nic = process.argv[2];
}
shell.exec('ifconfig ' + nic, function (err, stdout, stderr) {
var nics = new Nics(stdout.toString());
nics.nics.forEach(function (nic) {
if (nic.ip4) {
console.log(nic.name + ": " + nic.ip4);
}
});
return;
console.log(sys.inspect(nics));
console.log(sys.inspect(nics.getActive()));
});
function Nics(text) {
var self = this;
this.nics = [];
this.getActive = function () {
var active = [];
self.nics.forEach(function (nic) {
if (nic.status === 'active') {
active.push(nic);
}
});
return active;
};
var nicRegEx = /^(\w*\d)(?:[\:])/gm;
var nicIndexes = [];
var match;
while (match !== null) {
match = nicRegEx.exec(text);
if (match !== null) {
nicIndexes.push({ name: match[1], index: match.index });
}
}
for (var index = index || 0; index < nicIndexes.length; index += 1) {
var startIndex = nicIndexes[index].index + nicIndexes[index].name.length + 2;
var endIndex = ((nicIndexes[index + 1] || { index: null }).index || text.length) - 1;
self.nics.push(new Nic(nicIndexes[index].name, text.substr(startIndex, endIndex - startIndex)));
// console.log(sys.inspect(new Nic(nicIndexes[index].name, output.substr(startIndex, endIndex - startIndex))));
// console.log('|' + output.substr(startIndex, endIndex - startIndex) + '|');
}
// console.log(sys.inspect(matches));
//console.log(output, sys.inspect(nicIndexes));
}
function Nic(name, text) {
var self = this;
this.name = name;
this.ip4 = '';
//this.text = text;
var nicRegEx = /^\t(\w+)(?:[\:]?[ ])/gm;
var nicIndexes = [];
var match;
while (match !== null) {
match = nicRegEx.exec(text);
if (match !== null) {
// console.log(sys.inspect(match));
nicIndexes.push({ name: match[1], keyIndex: match.index, valueIndex: match.index + match[0].length });
}
}
// console.log(sys.inspect(nicIndexes));
for (var index = 0; index < nicIndexes.length; index += 1) {
var startIndex = nicIndexes[index].valueIndex;
var endIndex = (nicIndexes[index + 1] || { keyIndex: text.length }).keyIndex;
var value = this[nicIndexes[index].name] = text.substr(startIndex, endIndex - startIndex).replace(/[\n\t]/, '');
if ( nicIndexes[index].name === 'inet') {
self.ip4 = value.match(/\d{1,3}[\.]\d{1,3}[\.]\d{1,3}[\.]\d{1,3}/)[0];
}
// console.log(nicIndexes[index].name, text.substr(startIndex, endIndex - startIndex).replace(/[\n\t]/, ''));
}
}