[Twisted-Python] Simple ssh problem, please help
Hi everyone, I am a beginner to Twisted so any help would be appreciated. I got an example code from the Twisted, Networking Programming Essentials book: from twisted.conch import error from twisted.conch.ssh import transport, connection, keys, userauth, channel, common from twisted.internet import defer, protocol, reactor class ClientCommandTransport(transport.SSHClientTransport): def __init__(self, username, password, command): self.username = username self.password = password self.command = command print "Transport Created" def verifyHostKey(self, pubKey, fingerprint): print "In Verify Host Key" return defer.succeed(True) def connectionSecure(self): print "In Connection Secure" temp = PasswordAuth(self.username, self.password, ClientConnection(self.command)) print "Created Everything" self.requestService(temp) print "Double Check" class PasswordAuth(userauth.SSHUserAuthClient): def __init__(self, user, password, connection): userauth.SSHUserAuthClient.__init__(self, user, connection) self.password = password print "init password auth" def getPassword(self, prompt=None): print "In getPassword" return defer.succeed(self.password) class ClientConnection(connection.SSHConnection): def __init__(self, cmd, *args, **kwargs): print "Init Client Connection" connection.SSHConnection.__init__(self) print "Next line of Client Connection" self.command = cmd def serviceStarted(self): print "Service Started" self.openChannel(CommandChannel(self.command, conn=self)) class CommandChannel(channel.SSHChannel): name = 'session' def __init__(self, command, *args, **kwargs): channel.SSHChannel.__init__(self, *args, **kwargs) self.command = command def channelOpen(self, data): print ("Channel was opened") self.conn.sendRequest(self,'exec',common.NS(self.command),wantReply = True).addCallback(self._gotResponse) def _gotResponse(self,_): self.conn.sendEOF(self) def dataReceived(self, data): print data def closed(self): print("###### Entered Closed #####") self.loseConnection() reactor.stop() print("##### Done with Closed ####") class ClientCommandFactory(protocol.ClientFactory): def __init__(self, username, password, command): self.username = username self.password = password self.command = command def buildProtocol(self,addr): protocol = ClientCommandTransport( self.username, self.password, self.command) return protocol if __name__ == "__main__": import sys, getpass server = sys.argv[1] command = sys.argv[2] username = raw_input("Username: ") password = getpass.getpass("Password: ") factory = ClientCommandFactory(username, password, command) reactor.connectTCP(server, 22, factory) reactor.run() ************************************************************* But after running this, I get this Unhandled error. Which is strange because the reactor is being successfully stopped. Does anyone have any clue what this means and how I should go about fixing it? Thanks!!!!! Unhandled Error Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.py", line 586, in _doReadOrWrite why = selectable.doRead() File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 199, in doRead rval = self.protocol.dataReceived(data) File "/usr/lib/python2.7/dist-packages/twisted/conch/ssh/transport.py", line 438, in dataReceived self.dispatchMessage(messageNum, packet[1:]) File "/usr/lib/python2.7/dist-packages/twisted/conch/ssh/transport.py", line 460, in dispatchMessage messageNum, payload) --- <exception caught here> --- File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 84, in callWithLogger return callWithContext({"system": lp}, func, *args, **kw) File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 69, in callWithContext return context.call({ILogContext: newCtx}, func, *args, **kw) File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 118, in callWithContext return self.currentContext().callWithContext(ctx, func, *args, **kw) File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 81, in callWithContext return func(*args,**kw) File "/usr/lib/python2.7/dist-packages/twisted/conch/ssh/service.py", line 44, in packetReceived return f(packet) File "/usr/lib/python2.7/dist-packages/twisted/conch/ssh/connection.py", line 314, in ssh_CHANNEL_REQUEST channel = self.channels[localChannel] exceptions.KeyError: 0
participants (1)
-
Kevin Ting