networkGatewayDefault() added

This commit is contained in:
Sebastian Hildebrandt 2019-11-11 19:53:10 +01:00
parent 9f38d346dd
commit 09a2a901f3
6 changed files with 84 additions and 1 deletions

View File

@ -435,6 +435,7 @@ I also created a nice little command line tool called [mmon][mmon-github-url] (
| | [0].speed | X | | X | X | | speed in MBit / s |
| | [0].carrierChanges | X | | | | | # changes up/down |
| si.networkInterfaceDefault(cb) | : string | X | X | X | X | X | get name of default network interface |
| si.networkGatewayDefault(cb) | : string | X | X | X | X | X | get default network gateway |
| si.networkStats(ifaces,cb) | [{...}] | X | X | X | X | | current network stats of given interfaces<br>iface list: space or comma separated<br>iface parameter is optional<br>defaults to first external network interface,<br />Pass '*' for all interfaces |
| | [0].iface | X | X | X | X | | interface |
| | [0].operstate | X | X | X | X | | up / down |

View File

@ -191,7 +191,7 @@
</div>
<div class="row number-section">
<div class="col-xl-4 col-lg-4 col-md-4 col-12">
<div class="numbers">9,371</div>
<div class="numbers">9,445</div>
<div class="title">Lines of code</div>
</div>
<div class="col-xl-4 col-lg-4 col-md-4 col-12">

View File

@ -215,6 +215,16 @@
<td>X</td>
<td>get name of default network interface</td>
</tr>
<tr>
<td>si.networkGatewayDefault(cb)</td>
<td>: string</td>
<td>X</td>
<td>X</td>
<td>X</td>
<td>X</td>
<td>X</td>
<td>get default network gateway</td>
</tr>
<tr>
<td>si.networkStats(iface,cb)</td>
<td>[{...}]</td>

1
lib/index.d.ts vendored
View File

@ -643,6 +643,7 @@ export function disksIO(cb?: (data: Systeminformation.DisksIoData) => any): Prom
export function diskLayout(cb?: (data: Systeminformation.DiskLayoutData[]) => any): Promise<Systeminformation.DiskLayoutData[]>;
export function networkInterfaceDefault(cb?: (data: string) => any): Promise<string>;
export function networkGatewayDefault(cb?: (data: string) => any): Promise<string>;
export function networkInterfaces(cb?: (data: Systeminformation.NetworkInterfacesData[]) => any): Promise<Systeminformation.NetworkInterfacesData[]>;
export function networkStats(ifaces?: string, cb?: (data: Systeminformation.NetworkStatsData[]) => any): Promise<Systeminformation.NetworkStatsData[]>;

View File

@ -347,6 +347,7 @@ exports.disksIO = filesystem.disksIO;
exports.diskLayout = filesystem.diskLayout;
exports.networkInterfaceDefault = network.networkInterfaceDefault;
exports.networkGatewayDefault = network.networkGatewayDefault;
exports.networkInterfaces = network.networkInterfaces;
exports.networkStats = network.networkStats;
exports.networkConnections = network.networkConnections;

View File

@ -1002,3 +1002,73 @@ function networkConnections(callback) {
}
exports.networkConnections = networkConnections;
function networkGatewayDefault(callback) {
return new Promise((resolve) => {
process.nextTick(() => {
let result = '';
if (_linux || _freebsd || _openbsd || _netbsd) {
let cmd = 'ip route get 1';
try {
exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\n');
const line = lines && lines[0] ? lines[0] : '';
let parts = line.split(' via ');
if (parts && parts[1]) {
parts = parts[1].split(' ');
result = parts[0];
}
if (callback) {
callback(result);
}
resolve(result);
}
});
} catch (e) {
if (callback) { callback(result); }
resolve(result);
}
}
if (_darwin) {
let cmd = 'route -n get default';
try {
exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\n').map(line => line.trim());
result = util.getValue(lines, 'gateway');
if (callback) {
callback(result);
}
resolve(result);
}
});
} catch (e) {
if (callback) { callback(result); }
resolve(result);
}
}
if (_windows) {
try {
util.powerShell('Get-CimInstance -ClassName Win32_IP4RouteTable | Where-Object { $_.Destination -eq \'0.0.0.0\' -and $_.Mask -eq \'0.0.0.0\' }).InterfaceIndex')
.then(data => {
let lines = data.toString().split('\r\n');
result = lines && lines[0] ? lines[0] : '';
if (callback) {
callback(result);
}
resolve(result);
});
} catch (e) {
if (callback) { callback(result); }
resolve(result);
}
}
});
});
}
exports.networkGatewayDefault = networkGatewayDefault;