[Twisted-Python] Passing revieved rata and erroring out bad passowrds

Brian Levin b.levin at hvcc.edu
Tue Oct 28 10:42:49 EDT 2008

I apologizes if these are simple questions.  I am a complete newb when it comes to python and twisted.  I am trying to pass a command via ssh to a server.  I am having 2 problems.  I am hoping that someone can help me.  I have been unable to find answers in "the google".  

Problem 1: I am unable to pass the received data to a variable without creating a global.  As a general rule it is my understanding that the use of globals in python should be avoided.  If there is a better method for passing the data variable out of the dataReceived method I would prefer to use it.

Problem 2:  A bad password or username causes and infinite loop.  I need to find a way to cause an error or return a false value when the password or username is bad.

I have included my code below.  Any help would be most appreciated.  Thank you in advance

--Brian Levin


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

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):
		global test
		test = data

	def closed(self):

USER = raw_input('User: ')
HOST = raw_input('Host: ')
CMD = raw_input('CMD: ')
protocol.ClientCreator(reactor, Transport).connectTCP(HOST, 22)
print test


More information about the Twisted-Python mailing list