Skip to content

Instantly share code, notes, and snippets.

@gekowa
Created March 1, 2013 04:41
Show Gist options
  • Save gekowa/5062534 to your computer and use it in GitHub Desktop.
Save gekowa/5062534 to your computer and use it in GitHub Desktop.
Usage: node node-ping-graph.js <IP Address> Works only on Windows.
var cols = process.stdout.columns,
rows = process.stdout.rows,
spawn = require('child_process').spawn,
i, j,
red = '\u001b[31m',
blue = '\u001b[34m',
green = '\u001b[32m',
white = '\u001b[37m',
greenBg = '\u001b[42m',
redBg = '\u001b[41m',
reset = '\u001b[0m',
maxPing = 1000,
pingTarget,
ping,
pingResultStream = "",
skipped = 0,
timeOutText = "Target is timed out.",
fillChar = " ";
pingTarget = process.argv[2];
ping = spawn('ping', [pingTarget, '-t']);
process.title = pingTarget;
if (pingTarget === undefined) {
return null;
}
ping.stdout.on('data', function (data) {
var dataString = data.toString(),
pingResult,
timeMatch,
time,
timeText,
noOfBars,
bars;
if (dataString.indexOf("\r\n") >= 0) {
// skip first 2 lines
if (skipped <= 2) {
skipped ++;
return;
}
pingResultStream += dataString;
pingResult = pingResultStream;
pingResultStream = "";
// analyze
// console.log(pingResult);
if (/time\=\d+ms/.test(pingResult)) {
timeMatch = pingResult.match(/time\=(\d+)ms/);
time = timeMatch[1];
timeText = time + "ms";
noOfBars = cols * time / maxPing;
bars = "";
bars += greenBg;
for (i = 0; i < noOfBars / 2 - timeText.length / 2; i++) {
bars += " ";
}
bars += (white + timeText);
bars += greenBg;
for (j = i; j < noOfBars - timeText.length ; j++) {
bars += " ";
}
process.stdout.write(bars);
process.stdout.write(reset);
if (noOfBars < cols) {
process.stdout.write("\n");
}
} else if (/timed out/.test(pingResult)) {
bars = "";
for (i = 0; i < cols / 2 - timeOutText.length / 2; i++) {
bars += " ";
}
bars += (white + timeOutText);
bars += redBg;
for (j = i; j < cols - timeOutText.length - 1 ; j++) {
bars += " ";
}
console.log(redBg + bars + reset);
}
} else {
pingResultStream += dataString;
// console.log("HALF ");
}
// console.log(data.toString()); // + "->" + data.toString().indexOf("\r\n"));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment