Differences between revisions 1 and 2
Revision 1 as of 2008-06-15 06:43:49
Size: 1635
Editor: ZoomQuiet
Comment:
Revision 2 as of 2008-06-15 06:44:14
Size: 1650
Editor: ZoomQuiet
Comment:
Deletions are marked like this. Additions are marked like this.
Line 38: Line 38:
== 远程 == == 远程~paramiko模块 ==

TableOfContents

Include(ZPyUGnav)

Py模拟远程tail

本地

俊杰蔡 <[email protected]>
reply-to        [email protected]
to      [email protected]
date    Fri, Jun 13, 2008 at 15:23
subject [CPyUG:54525] 远程tail -f功能的问题
  • 如何用python来实现远程tail -f的功能? 各位有什么好的思路么? 本地模拟tail -f功能可以这样:

   1     file = open("access_log.2008061311","r")
   2     while 1:
   3         where = file.tell()
   4         content = file.readlines()
   5         if not content:
   6             time.sleep(1)
   7             file.seek(where)
   8         else:
   9             print content
  • 我初步想到的一个办法是把上面的代码放到B机器上运行,A机器起个监听程序,接收B机器返回的内容。但这样不够pythonic。有没有更加好的方法呢?

远程~paramiko模块

paramiko模块基本可以满足要求

   1 import paramiko as ssh
   2 client=ssh.SSHClient()
   3 client.load_system_host_keys()
   4 client.connect(host,22,username,"",None,None)
   5 stdin, stdout, tderr = self.client.exec_command("tail -f log")
   6 line = stdout.readline()
   7 while line:
   8      #myTime = strftime('%H:%M:%S', localtime())
   9      print("[%s] %s" % (self.host,line))
  10      line = stdout.readline()


反馈

创建 by -- ZoomQuiet [DateTime(2008-06-15T06:43:49Z)]

PageComment2

[:/PageCommentData:PageCommentData]

MiscItems/2008-06-15 (last edited 2009-12-25 07:09:50 by localhost)