-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (50 loc) · 1.51 KB
/
index.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
var
ChildProcess = require('child_process'),
Q = require('q');
var execute = module.exports.execute = function(handle, options){
var deferred = Q.defer();
if ( 'undefined' === typeof options ) {
options = {};
}
ChildProcess.exec(handle, options, function (error, stdout, stderr) {
if (error) {
deferred.reject(error);
} else if (stderr) {
deferred.reject(stderr);
} else {
deferred.resolve(stdout);
}
});
return deferred.promise;
};
module.exports.getGatewayArp = function(){
return execute('netstat -rn', { timeout: 2000 })
.then(function(netstat){
var
defaultLine = /default.*([0-9]{1,3}\.){3}[0-9]{1,3}/.exec(netstat),
defaultIP;
if ( null === defaultLine ) {
throw 'Could not find default line in netstat';
} else {
defaultLine = defaultLine[0];
}
defaultIP = /([0-9]{1,3}\.){3}[0-9]{1,3}/.exec(defaultLine);
if ( null === defaultIP ) {
throw 'Could not find default IP';
} else {
defaultIP = defaultIP[0];
}
return defaultIP;
})
.then(function(defaultIP){
return execute('arp -n '+ defaultIP, { timeout: 2000 })
.then(function(arp){
var defaultMac = /([0-9A-F]{1,2}[:-]){5}[0-9A-F]{1,2}/i.exec(arp);
if ( null === defaultMac ) {
throw 'Could not find Gateway address in arp table';
}
defaultMac = defaultMac[0];
return [defaultMac, defaultIP];
});
});
};