work in progress: transport, esp filetransfer with sftp

git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@2650 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2008-05-30 10:18:47 +00:00
parent 9754f0e22d
commit d319203a4f
4 changed files with 44 additions and 27 deletions

View file

@ -17,8 +17,8 @@
# #
""" """
Providing access for remote cybertools.agent instances by listening Providing access for remote agent instances by listening for requests
for requests from client agents. from remote transport agents.
$Id$ $Id$
""" """

View file

@ -17,18 +17,18 @@
# #
""" """
Transferring files to a remote site via SCP. Transferring files to a remote site via SFTP.
$Id$ $Id$
""" """
from twisted.conch.ssh import connection from twisted.conch.ssh import channel, common, connection
from twisted.conch.ssh import filetransfer, transport, userauth from twisted.conch.ssh import filetransfer, transport, userauth
from twisted.internet import defer, protocol, reactor from twisted.internet import defer, protocol, reactor
class FileTransferConnection(protocol.ClientFactory): class FileTransfer(protocol.ClientFactory):
""" Transfers files to a remote SCP server. """ Transfers files to a remote SCP/SFTP server.
""" """
def __init__(self, host, port, username, password): def __init__(self, host, port, username, password):
@ -38,60 +38,77 @@ class FileTransferConnection(protocol.ClientFactory):
reactor.connectTCP(host, port, self) reactor.connectTCP(host, port, self)
def buildProtocol(self, addr): def buildProtocol(self, addr):
protocol = self.protocol = ClientTransport(self.username, self.password) protocol = self.protocol = ClientTransport(self)
return protocol return protocol
def copyToRemote(self, localPath, remotePath): def copyToRemote(self, localPath, remotePath):
""" Copies a file, returning a deferred. """ Copies a file, returning a deferred.
""" """
d = defer.Deferred() d = defer.Deferred()
self.queue.append((localPath, remotePath, d)) self.queue.append(dict(deferred=d,
#d = self.protocol.openFile('text.txt', filetransfer.FXF_WRITE, {}) command='copyToRemote',
#d.addCallback(self.write) localPath=localPath,
remotePath=remotePath))
return d return d
def write(self, file):
file.writeChunk(0, 'hello')
file.close()
return 'Done'
def close(self): def close(self):
# TODO: put in queue...
self.protocol.transport.loseConnection() self.protocol.transport.loseConnection()
print 'connection closed' print 'connection closed'
class ClientTransport(transport.SSHClientTransport): class ClientTransport(transport.SSHClientTransport):
def __init__(self, username, password): def __init__(self, factory):
self.username = username self.factory = factory
self.password = password
def verifyHostKey(self, pubKey, fingerprint): def verifyHostKey(self, pubKey, fingerprint):
# this is insecure!!! # this is insecure!!!
return defer.succeed(True) return defer.succeed(True)
def connectionSecure(self): def connectionSecure(self):
self.requestService(UserAuth(self.username, self.password, self.requestService(UserAuth(self.factory, ClientConnection(self.factory)))
ClientConnection()))
class ClientConnection(connection.SSHConnection): class ClientConnection(connection.SSHConnection):
def __init__(self, factory):
connection.SSHConnection.__init__(self)
self.factory = factory
def serviceStarted(self): def serviceStarted(self):
self.openChannel(SFTPChannel(conn=self)) self.openChannel(SFTPChannel(conn=self))
class SFTPChannel(channel.SSHChannel): class SFTPChannel(channel.SSHChannel):
name = 'session'
def channelOpen(self, data):
d = self.conn.sendRequest(self, 'subsystem', common.NS('sftp'), wantReply=1)
d.addCallback(self.channelOpened)
def channelOpened(self, data): def channelOpened(self, data):
self.client = filetransfer.FileTransferClient print 'channelOpened', data
self.client = filetransfer.FileTransferClient()
self.client.makeConnection(self)
self.dataReceived = self.client.dataReceived
self.execute()
def execute(self):
queue = self.conn.factory.queue
print 'execute, queue =', queue
def copyToRemote(self, params):
remotePath = params['remotePath']
d = self.protocol.openFile(remotePath, filetransfer.FXF_WRITE, {})
class UserAuth(userauth.SSHUserAuthClient): class UserAuth(userauth.SSHUserAuthClient):
def __init__(self, user, password, connection): def __init__(self, factory, connection):
userauth.SSHUserAuthClient.__init__(self, user, connection) userauth.SSHUserAuthClient.__init__(self, factory.username, connection)
self.password = password self.password = factory.password
def getPassword(self, prompt=None): def getPassword(self, prompt=None):
return defer.succeed(self.password) return defer.succeed(self.password)

View file

@ -17,8 +17,7 @@
# #
""" """
Transferring information to an application on the same machine, typically Transferring information to a loops site on a local Zope instance.
a loops site on a local Zope instance.
$Id$ $Id$
""" """

View file

@ -18,7 +18,8 @@
""" """
Transferring information to or requesting information from a remote Transferring information to or requesting information from a remote
cybertools.agent instance with a corresponding server agent. cybertools.agent instance by transferring files to the remote system
and sending requests to a corresponding remote controller.
$Id$ $Id$
""" """