python类允许通过ssh跟踪多个文件。
python-sshtail的Python项目详细描述
一组简单的python类,便于通过ssh跟踪一个或多个文件。 目前它只支持基于密钥的ssh'ing。
快速安装
从pypi安装:
> easy_install -U python-sshtail
跟踪单个文件
from sshtail import SSHTailer from time import sleep # "1.2.3.4" is the IP address or host name you want to access tailer = SSHTailer('1.2.3.4', '/var/log/path/to/my/logfile.log') try: while 1: for line in tailer.tail(): print line # wait a bit time.sleep(1) except: tailer.disconnect()
跟踪多个文件
from sshtail import SSHMultiTailer tailer = SSHMultiTailer({ '1.2.3.4': ['/path/to/log1.log', '/path/to/log2.log'], '4.3.2.1': ['/path/to/log3.log'], }) # will run until it receives SIGINT, after which it will # automatically catch the exception, disconnect from the # remote hosts and perform cleanup for host, filename, line in tailer.tail(): print "%s:%s - %s" % (host, filename, line)
使用自定义私钥
from sshtail import SSHMultiTailer, load_dss_key # if no path's specified for the private key file name, # it automatically prepends /home/<current_user>/.ssh/ # and for RSA keys, import load_rsa_key instead. tailer = SSHMultiTailer({ '1.2.3.4': ['/path/to/log1.log', '/path/to/log2.log'], '4.3.2.1': ['/path/to/log3.log'], }, private_key=load_dss_key('identity')) for host, filename, line in tailer.tail(): print "%s:%s - %s" % (host, filename, line)