使用 Twisted 框架进行网络编程,第 4 部分
2008-11-13 13:13:47 来源:WEB开发网清单1. ssh-weblog.py
#!/usr/bin/env python
"""Monitor a remote weblog over SSH
USAGE: ssh-weblog.py user@host logfile
"""
from twisted.conch.ssh import transport, userauth, connection, channel
from twisted.conch.ssh.common import NS
from twisted.internet import defer, protocol, reactor
from twisted.python import log
from getpass import getpass
import struct, sys, os
import webloglib as wll
USER,HOST,CMD = None,None,None
class Transport(transport.SSHClientTransport):
def verifyHostKey(self, hostKey, fingerprint):
print 'host key fingerprint: %s' % fingerprint
return defer.succeed(1)
def connectionSecure(self):
self.requestService(UserAuth(USER, Connection()))
class UserAuth(userauth.SSHUserAuthClient):
def getPassword(self):
return defer.succeed(getpass("password: "))
def getPublicKey(self):
return # Empty implementation: always use password auth
class Connection(connection.SSHConnection):
def serviceStarted(self):
self.openChannel(Channel(2**16, 2**15, self))
class Channel(channel.SSHChannel):
name = 'session' # must use this exact string
def openFailed(self, reason):
print '"%s" failed: %s' % (CMD,reason)
def channelOpen(self, data):
self.welcome = data # Might display/process welcome screen
d = self.conn.sendRequest(self,'exec',NS(CMD),wantReply=1)
def dataReceived(self, data):
recs = data.strip().split('n')
for rec in recs:
hit = [field.strip('"') for field in wll.log_fields(rec)]
resource = hit[wll.request].split()[1]
referrer = hit[wll.referrer]
if resource=='/kill-weblog-monitor':
print "Bye bye..."
self.closed()
return
elif hit[wll.status]=='200' and hit[wll.referrer]!='-':
print referrer, ' -->', resource
def closed(self):
self.loseConnection()
reactor.stop()
if __name__=='__main__':
if len(sys.argv) < 3:
sys.stderr.write('__doc__')
sys.exit()
USER, HOST = sys.argv[1].split('@')
CMD = 'tail -f -n 1 '+sys.argv[2]
protocol.ClientCreator(reactor, Transport).connectTCP(HOST, 22)
reactor.run()
- ››使用脚本恢复WinXP系统的用户登录密码
- ››使用phpMyadmin创建数据库及独立数据库帐号
- ››使用Zend Framework框架中的Zend_Mail模块发送邮件...
- ››使用cout标准输出如何控制小数点后位数
- ››使用nofollow标签做SEO的技巧
- ››使用 WebSphere Message Broker 的 WebSphere Tra...
- ››使用SQL Server事件探查器做应用程序的性能分析
- ››使用SQL Server事件探查器分析死锁原因
- ››使用纯文本文件打造WCF服务
- ››使用 Dojo 开发定制 Business Space 小部件,第 4...
- ››使用 ADDRESS 与 INDIRECT函数查询信息
- ››使用 COLUMN函数编制单元信息
更多精彩
赞助商链接