Get default gateway

This commit is contained in:
Maria Camila Cubides 2021-05-04 12:02:22 -05:00
parent 9c1a6f47f7
commit f25bb4e25d
2 changed files with 24 additions and 9 deletions

View File

@ -1505,17 +1505,24 @@ function networkGatewayDefault(callback) {
if (!error) {
let lines = stdout.toString().split('\n').map(line => line.trim());
result = util.getValue(lines, 'gateway');
if (callback) {
callback(result);
if (!result) {
cmd = 'netstat -rn | awk \'/default/ {print $2}\'';
exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) {
lines = stdout.toString().split('\n').map(line => line.trim());
result = lines.find(line => (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(line)));
if (callback) {
callback(result);
}
resolve(result);
});
} else {
if (callback) {
callback(result);
}
resolve(result);
}
resolve(result);
} else {
if (callback) {
callback(result);
}
resolve(result);
}
});
});
} catch (e) {
if (callback) { callback(result); }
resolve(result);

8
test/networktest.js Normal file
View File

@ -0,0 +1,8 @@
const network = require('../lib/network');
async function getNetwork() {
console.log(await network.networkGatewayDefault());
}
getNetwork();