-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (46 loc) · 1.44 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
const singleCommand = require( './singleCommand' );
/** @type {Boolean} */
const windows = /^win/.test( process.platform );
/**
@function
@param {String} host
@param {Object} [options]
@return {Promise} */
module.exports = async ( host, options = {}) => {
/** @type {integer} */
const packetsCount = options.packetsCount || 4;
/** @type {String} */
let command = windows
? `chcp 437 && ping -n ${packetsCount} ` + host
: `ping -c ${packetsCount} ` + host;
let string;
try{
/** @type {String} */
string = await singleCommand( command );
}
catch( error ){
let { consoleOutput } = error;
if( !consoleOutput ) throw error;
string = consoleOutput;
}
/** @type {Array<String>} */
let lines = string.trim().split( '\n' )
.map( line => line.trim() )
.filter( line => line );
let total, received;
if( windows ){
/** @type {String} */
let line = lines.find( line => line.includes( 'Packets' ) );
[ total, received ] = line.split( '=' ).slice( 1, 3 )
.map( item => Number( item.split( ',' )[ 0 ].trim() ) );
//Packets: Sent = 8, Received = 8, Lost = 0 (0% loss),
}
else{
/** @type {String} */
let line = lines.find( line => line.includes( 'packets' ) );
[ total, received ] = line.split( ',' ).slice( 0, 2 )
.map( item => Number( item.trim().split( ' ' )[ 0 ] ) );
//4 packets transmitted, 4 received, 0% packet loss, time 3056ms
}
return { total, received };
};