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
for requests from client agents.
Providing access for remote agent instances by listening for requests
from remote transport agents.
$Id$
"""

View file

@ -17,18 +17,18 @@
#
"""
Transferring files to a remote site via SCP.
Transferring files to a remote site via SFTP.
$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.internet import defer, protocol, reactor
class FileTransferConnection(protocol.ClientFactory):
""" Transfers files to a remote SCP server.
class FileTransfer(protocol.ClientFactory):
""" Transfers files to a remote SCP/SFTP server.
"""
def __init__(self, host, port, username, password):
@ -38,60 +38,77 @@ class FileTransferConnection(protocol.ClientFactory):
reactor.connectTCP(host, port, self)
def buildProtocol(self, addr):
protocol = self.protocol = ClientTransport(self.username, self.password)
protocol = self.protocol = ClientTransport(self)
return protocol
def copyToRemote(self, localPath, remotePath):
""" Copies a file, returning a deferred.
"""
d = defer.Deferred()
self.queue.append((localPath, remotePath, d))
#d = self.protocol.openFile('text.txt', filetransfer.FXF_WRITE, {})
#d.addCallback(self.write)
self.queue.append(dict(deferred=d,
command='copyToRemote',
localPath=localPath,
remotePath=remotePath))
return d
def write(self, file):
file.writeChunk(0, 'hello')
file.close()
return 'Done'
def close(self):
# TODO: put in queue...
self.protocol.transport.loseConnection()
print 'connection closed'
class ClientTransport(transport.SSHClientTransport):
def __init__(self, username, password):
self.username = username
self.password = password
def __init__(self, factory):
self.factory = factory
def verifyHostKey(self, pubKey, fingerprint):
# this is insecure!!!
return defer.succeed(True)
def connectionSecure(self):
self.requestService(UserAuth(self.username, self.password,
ClientConnection()))
self.requestService(UserAuth(self.factory, ClientConnection(self.factory)))
class ClientConnection(connection.SSHConnection):
def __init__(self, factory):
connection.SSHConnection.__init__(self)
self.factory = factory
def serviceStarted(self):
self.openChannel(SFTPChannel(conn=self))
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):
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):
def __init__(self, user, password, connection):
userauth.SSHUserAuthClient.__init__(self, user, connection)
self.password = password
def __init__(self, factory, connection):
userauth.SSHUserAuthClient.__init__(self, factory.username, connection)
self.password = factory.password
def getPassword(self, prompt=None):
return defer.succeed(self.password)

View file

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

View file

@ -18,7 +18,8 @@
"""
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$
"""