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

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 <code> 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): self.loseConnection() reactor.stop() USER = raw_input('User: ') HOST = raw_input('Host: ') CMD = raw_input('CMD: ') protocol.ClientCreator(reactor, Transport).connectTCP(HOST, 22) reactor.run() print test </code>

On Tue, 28 Oct 2008 10:42:49 -0400 (EDT), Brian Levin <b.levin@hvcc.edu> wrote:
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.
I'm not sure what you mean by "pass the received data to a variable". In general, I think that it only makes sense to pass objects to functions. What you do to variables is set them. From looking at your code, I'm not sure what you want to accomplish. Your code sets a global variable and later, when your program is done and about to exit, prints the value. If that's all you want to do, you could just print the value instead of setting a variable to it. I suspect you want to do something else, but I don't know what. Whatever it is, it will probably involve calling a function or method of some object, probably passing the data you received (or some transformation of it) as an argument.
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'm not sure about this one. Jean-Paul

For the password problem, I think what you want to do is raise an exception, which will in turn cause your defereds to call their error backs. In the error back you can handle the particular error however you need to. That's probably something you need to either setup in the getpass module, alternatively, that may be setup already and just waiting for you to addErrback() on it. - Andy Fundinger On Tue, Oct 28, 2008 at 11:19 AM, Jean-Paul Calderone <exarkun@divmod.com> wrote:
-- Blog: http://channel3b.wordpress.com Second Life Name: Ciemaar Flintoff Watch out for the invisible man.

Assuming the getpass is the one the from the std lib, I don't see why you're overriding getPassword at all. according to the API docs, you should be able to get a custom prompt just by adding a prompt parameter: http://twistedmatrix.com/documents/8.1.0/api/twisted.conch.ssh.userauth.SSHU... and you probably don't want to call succeed() on the deferred you are returning, since that likely screws up the whole auth calback chain if you were looking at this example: http://www.devshed.com/c/a/Python/SSH-with-Twisted/4/ realize that it might be misleading, as the author overrides the __init__ of his SSHUserAuthClient and passes the password in directly (he does not prompt the user for it) ...or I could be misunderstanding the way the module works... :) Good Luck! Kevin Horn On Tue, Oct 28, 2008 at 2:36 PM, Andy Fundinger <Andy@newworldelectric.com>wrote:

On Tue, 28 Oct 2008 10:42:49 -0400 (EDT), Brian Levin <b.levin@hvcc.edu> wrote:
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.
I'm not sure what you mean by "pass the received data to a variable". In general, I think that it only makes sense to pass objects to functions. What you do to variables is set them. From looking at your code, I'm not sure what you want to accomplish. Your code sets a global variable and later, when your program is done and about to exit, prints the value. If that's all you want to do, you could just print the value instead of setting a variable to it. I suspect you want to do something else, but I don't know what. Whatever it is, it will probably involve calling a function or method of some object, probably passing the data you received (or some transformation of it) as an argument.
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'm not sure about this one. Jean-Paul

For the password problem, I think what you want to do is raise an exception, which will in turn cause your defereds to call their error backs. In the error back you can handle the particular error however you need to. That's probably something you need to either setup in the getpass module, alternatively, that may be setup already and just waiting for you to addErrback() on it. - Andy Fundinger On Tue, Oct 28, 2008 at 11:19 AM, Jean-Paul Calderone <exarkun@divmod.com> wrote:
-- Blog: http://channel3b.wordpress.com Second Life Name: Ciemaar Flintoff Watch out for the invisible man.

Assuming the getpass is the one the from the std lib, I don't see why you're overriding getPassword at all. according to the API docs, you should be able to get a custom prompt just by adding a prompt parameter: http://twistedmatrix.com/documents/8.1.0/api/twisted.conch.ssh.userauth.SSHU... and you probably don't want to call succeed() on the deferred you are returning, since that likely screws up the whole auth calback chain if you were looking at this example: http://www.devshed.com/c/a/Python/SSH-with-Twisted/4/ realize that it might be misleading, as the author overrides the __init__ of his SSHUserAuthClient and passes the password in directly (he does not prompt the user for it) ...or I could be misunderstanding the way the module works... :) Good Luck! Kevin Horn On Tue, Oct 28, 2008 at 2:36 PM, Andy Fundinger <Andy@newworldelectric.com>wrote:
participants (4)
-
Andy Fundinger
-
Brian Levin
-
Jean-Paul Calderone
-
Kevin Horn