-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
44 lines (32 loc) · 1.13 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
import childProcess from 'child_process';
import isString from 'lodash.isstring';
const handleIpRouteResults = (callback) => (error, stdout, stderr) => {
if (stdout && isString(stdout)) {
const output = stdout;
const match = output.match(/default via ((?:[0-9]{1,3}\.){3}[0-9]{1,3}) dev eth0/);
let ip = undefined;
if (Array.isArray(match) && match.length >= 2) {
ip = match[1];
}
if (ip) {
callback(undefined, ip);
} else {
callback(new Error("Unable to find ip, perhaps call while not within a Docker container"), undefined);
}
} else if (error) {
callback(error, undefined);
} else if (stderr) {
callback(new Error(stderr), undefined);
} else {
callback(new Error("No results or feedback given"), undefined);
}
};
export default function(callback) {
try {
childProcess.execFile("/sbin/ip", ["route"], handleIpRouteResults(callback));
} catch(error) {
if (callback) {
callback(error, undefined);
}
}
}