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如何克服带有空值的不可空bean级验证   “此行有多个标记”的authenticateUser上的java MySQLConnection错误   java处理Jackson异常   java Netbeans编辑器如何知道文件在撤消后不会被修改?   目标C相当于Java的ArrayList   java当需要int时,可以将short传递给方法调用吗?   drawString()方法上的java MouseListener   java服务器socket是执行此操作的最佳选项   java需要单个匹配bean,但找到了2:org。springframework。网状物servlet。mvc。方法注释。RequestMappingHandlerMapping   如何在Java中执行Windows命令?   安卓应用程序的java文件主机   java将所有spring引导属性都设置在内。将yml文件转换为系统属性   Java邮件中的html内联链接在邮件客户端中不起作用   Java二进制文件的bazel版本控制   java如何在自定义ListView中使用Intent   java(Spring MVC+Hibernate 4+Test 4)自动连线DAO返回NULL   java一次add()调用可以在Solr服务器中索引多少文档?   java如何获取Avro GenericRecord大小