##language:zh #pragma section-numbers off ##含有章节索引导航的 ZPyUG 文章通用模板 <> ## 默许导航,请保留 <> = Py模拟远程tail = ##startInc == 本地 == {{{ 俊杰蔡 reply-to python-cn@googlegroups.com to python-cn@googlegroups.com date Fri, Jun 13, 2008 at 15:23 subject [CPyUG:54525] 远程tail -f功能的问题 }}} 如何用python来实现远程tail -f的功能? 各位有什么好的思路么? 本地模拟tail -f功能可以这样: {{{#!python file = open("access_log.2008061311","r") while 1: where = file.tell() content = file.readlines() if not content: time.sleep(1) file.seek(where) else: print content }}} 我初步想到的一个办法是把上面的代码放到B机器上运行,A机器起个监听程序,接收B机器返回的内容。但这样不够pythonic。有没有更加好的方法呢? == 远程~paramiko模块 == paramiko模块基本可以满足要求 {{{#!python import paramiko as ssh client=ssh.SSHClient() client.load_system_host_keys() client.connect(host,22,username,"",None,None) stdin, stdout, tderr = self.client.exec_command("tail -f log") line = stdout.readline() while line: #myTime = strftime('%H:%M:%S', localtime()) print("[%s] %s" % (self.host,line)) line = stdout.readline() }}} ##endInc ---- '''反馈''' 创建 by -- ZoomQuiet [<>]