Skip to content

Instantly share code, notes, and snippets.

@akobashikawa
Last active December 13, 2017 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akobashikawa/1e6d906bc9d44f630206994874ab2fd4 to your computer and use it in GitHub Desktop.
Save akobashikawa/1e6d906bc9d44f630206994874ab2fd4 to your computer and use it in GitHub Desktop.
Internet test with ping in NodeJS
const ping = require('ping');
let host = '8.8.8.8';
if (process.argv[2]) {
host = process.argv[2];
} else {
console.log("Syntax: \nnode ping-test-ifchanges-host host");
}
console.log("Testing host: " + host);
let isAlivePrev = false;
let nowPrev = new Date();
function doPing() {
ping.sys.probe(host, isAlive => {
if (isAlive !== isAlivePrev) {
let now = new Date();
let diff = (now.getTime() - nowPrev.getTime())/1000;
let timestamp = now.toISOString() + ' after: ' + diff + ' s';
console.log("\n" + host + ': ' + (isAlive ? ' OK' : ' KO') + ' ' + timestamp);
isAlivePrev = isAlive;
nowPrev = now;
} else {
process.stdout.write('.');
}
});
}
setInterval(doPing, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment