Last active
December 13, 2017 17:36
-
-
Save akobashikawa/1e6d906bc9d44f630206994874ab2fd4 to your computer and use it in GitHub Desktop.
Internet test with ping in NodeJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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