Skip to content
This repository has been archived by the owner on May 22, 2021. It is now read-only.

Commit

Permalink
ping module
Browse files Browse the repository at this point in the history
  • Loading branch information
rhpijnacker committed May 7, 2020
1 parent 7881a6c commit f9f52d4
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/client/ping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import child_process from 'child_process';

export default function ping(target) {
return new Promise((resolve) => {
let min, avg, max;
const child = child_process.spawn('ping', [target, '-c', '10', '-A']);
child.stdout.on('data', function (msg) {
msg = msg.toString();
console.log(`"${msg}"`);
const match = msg.match(
/rtt min\/avg\/max\/mdev = ([\d.]+)\/([\d.]+)\/([\d.]+)\/([\d.]+) ms/
);
if (match) {
console.log('match', match)
min = parseFloat(match[1]);
avg = parseFloat(match[2]);
max = parseFloat(match[3]);
}
});
child.on('close', () => resolve({ min, avg, max }));
});
}

0 comments on commit f9f52d4

Please sign in to comment.