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)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Java:字符串。RTL设备语言用标志“+”格式化,数字后加符号   java GAE作为桌面应用程序(Swing)的服务提供商   java将InputStream转换为FileInputStream不适用于Apache POI   java外部Voronoi库“网格”:什么是草图和处理?   重载重写的泛型方法java   java显示组织上设置的错误。springframework。验证。jsp中的错误对象   java一些Spring模型属性没有显示在我的JSP中   java无法编译Guava 23的SimpleTimeLimiter示例   java如何更改JTree中的“根”目录名?   java如何在安卓中对相对布局产生连锁反应?   java错误:org。冬眠例外SQLGrammarException:无法提取结果集,dateAccessed是未知列   如何使用java监听JSON文件更新   由抽象封闭类创建的匿名内部类能否通过反射确定实现类?