Форум сайта python.su
Полазил на форуме, ничего не нашел по Twisted.conch
Есть код:
# -*- coding: utf-8 -*-
from twisted.conch import error
from twisted.conch.ssh import transport, connection, keys, userauth, channel, common
from twisted.internet import defer, protocol, reactor
from twisted.python import log
import sys
class ClientCommandTransport(transport.SSHClientTransport):
def __init__(self, username, password):
self.username = username
self.password = password
def verifyHostKey(self, pubKey, fingerprint):
return defer.succeed(True)
def connectionSecure(self):
self.requestService(
PasswordAuth(self.username, self.password, ClientConnection()))
class PasswordAuth(userauth.SSHUserAuthClient):
def __init__(self, user, password, connection):
userauth.SSHUserAuthClient.__init__(self, user, connection)
self.password = password
def getPassword(self, prompt=None):
return defer.succeed(self.password)
class ClientConnection(connection.SSHConnection):
def __init__(self, *args, **kwargs):
connection.SSHConnection.__init__(self)
def serviceStarted(self):
#t = 1
#while t == 1:
self.openChannel(CommandChannel(conn=self))
class CommandChannel(channel.SSHChannel):
name = 'session'
def __init__(self, *args, **kwargs):
channel.SSHChannel.__init__(self, *args, **kwargs)
self.command = raw_input("Command: ")
def channelOpen(self, data):
self.conn.sendRequest(self, 'exec', common.NS(self.command), wantReply=True).addCallback(self._gotResponse)
def _gotResponse(self, _):
self.catData = ''
self.conn.sendEOF(self)
def dataReceived(self, data):
self.catData += data
def closed(self):
print 'We got this:\n', self.catData
reactor.stop()
class ClientCommandFactory(protocol.ClientFactory):
def __init__(self, username, password):
self.username = username
self.password = password
def buildProtocol(self, addr):
protocol = ClientCommandTransport(self.username, self.password)
return protocol
if __name__ == "__main__":
import sys, getpass
server = 'www.srv.ru'
username = 'name'
password = 'password'
factory = ClientCommandFactory(username, password)
reactor.connectTCP(server, 22, factory)
log.startLogging(sys.stdout, setStdout=0)
reactor.run()
Офлайн
Кажется, то что вы пытаетесь сделать уже написано в twisted.conch.scripts.conch
Офлайн