Created
May 16, 2011 19:58
-
-
Save justinrainbow/975213 to your computer and use it in GitHub Desktop.
Tail a remote file on multiple servers with Node.js
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
var sys = require('sys'), | |
spawn = require('child_process').spawn, | |
// args from command line | |
filename, servers; | |
if (process.ARGV.length < 4) { | |
return sys.puts("Usage: node remote-tail.js filename server1 [serverN]"); | |
} | |
filename = process.ARGV[2]; | |
servers = process.ARGV.slice(3); | |
function writeData(host, data) { | |
console.log(host + ": " + data); | |
} | |
function readData(host, data) { | |
var lines = data.toString().split("\n") | |
for (var i = 0, len = lines.length; i < len; i++) { | |
if (lines[i].length > 0) { | |
writeData(host, lines[i]) | |
} | |
} | |
} | |
for (var x = 0, len = servers.length; x < len; x++) { | |
var server = servers[x]; | |
// Look at http://nodejs.org/api.html#_child_processes for detail. | |
var tail = spawn("ssh", [server, "tail", "-f", filename]); | |
tail.stdout.on("data", function(data) { | |
readData(server, data); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For anyone stumbling in here, you might want to check in https://www.npmjs.com/package/remote-tail