-
-
Save grzegorzblaszczyk/357989 to your computer and use it in GitHub Desktop.
RTail
This file contains hidden or 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
#!/usr/bin/env python | |
""" | |
Usage: | |
./rtail.py user@host:port:path/foo.log bar.log host2:port:/path/baz.log | |
""" | |
import optparse | |
import os | |
import re | |
import select | |
import subprocess | |
import sys | |
import time | |
def main(): | |
op = optparse.OptionParser() | |
options, args = op.parse_args() | |
streams = list() | |
for arg in args: | |
if re.match(r"^(.+@)?[a-zA-Z0-9.-]+:.+", arg): | |
# this is a remote location | |
hostname, port, path = arg.split(":", 2) | |
s = subprocess.Popen(["ssh", "-v", "-p", port, hostname, "tail -f " + path], stdout=subprocess.PIPE) | |
s.name = arg | |
streams.append(s) | |
else: | |
s = subprocess.Popen(["tail", "-f", arg], stdout=subprocess.PIPE) | |
s.name = arg | |
streams.append(s) | |
lastName = None | |
while True: | |
r, _, _ = select.select( | |
[stream.stdout.fileno() for stream in streams], [], []) | |
for fileno in r: | |
for stream in streams: | |
if stream.stdout.fileno() != fileno: | |
continue | |
data = os.read(fileno, 4096) | |
if not data: | |
streams.remove(stream) | |
break | |
if stream.name != lastName: | |
print "%s:" % stream.name | |
lastName = stream.name | |
sys.stdout.write(data) | |
break | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment