[Twisted-Python] problem enhancing conch sample
Henning.Ramm at mediapro-gmbh.de
Henning.Ramm at mediapro-gmbh.de
Tue Oct 11 05:40:00 MDT 2005
Hello again!
Last time I had the same problem with a telnet client; I dropped the code to start again with the conch ssh sample. I'm trying to find a way to execute more than one command, but I seem to mistunderstand something fundamental, at least the server closes the connection always after the first command.
This is my code, derived closely (and a bit simplified) from the example:
#!/usr/bin/env python
from twisted.conch.ssh import transport, userauth, connection, common, keys, channel
from twisted.internet import defer, protocol, reactor
from twisted.python import log
import struct, sys, getpass, os
USER = 'me' # replace this with a valid username
PASS = 'mypass' # password
HOST = 'localhost' # and a valid host
class SimpleTransport(transport.SSHClientTransport):
def verifyHostKey(self, hostKey, fingerprint):
# print 'host key fingerprint: %s' % fingerprint
return defer.succeed(1)
def connectionSecure(self):
self.requestService(
SimpleUserAuth(USER,
SimpleConnection()))
class SimpleUserAuth(userauth.SSHUserAuthClient):
def getPassword(self):
return defer.succeed(PASS)
def getGenericAnswers(self, name, instruction, questions):
print name
print instruction
answers = []
for prompt, echo in questions:
if echo:
answer = raw_input(prompt)
else:
answer = getpass.getpass(prompt)
answers.append(answer)
return defer.succeed(answers)
def getPublicKey(self):
return None
def getPrivateKey(self):
return None
class SimpleConnection(connection.SSHConnection):
def serviceStarted(self):
self.openChannel(CatChannel(2**16, 2**15, self))
class CatChannel(channel.SSHChannel):
name = 'session'
cmd = 'pwd'
commands = ['pwd','cd /','ls -al','exit']
pointer = 0
def openFailed(self, reason):
print 'echo failed', reason
def channelOpen(self, ignoredData):
self.data = ''
reactor.callLater(0, self.sendNextRequest)
def sendNextRequest(self):
cmd = self.commands[self.pointer]
print cmd
d = self.conn.sendRequest(self, 'exec', common.NS(cmd), wantReply = 1)
d.addCallback(self._cbRequest)
d.addErrback(log.msg)
def _cbRequest(self, ignored):
self.write('hello conch\n') # any sense?
self.pointer += 1
if self.commands[self.pointer] == 'exit':
self.conn.sendEOF(self)
else:
reactor.callLater(0, self.sendNextRequest)
def dataReceived(self, data):
self.data += data
def closed(self):
print 'got data: %s' % repr(self.data)
self.loseConnection()
reactor.stop()
def twisted_logger(logdict):
text = " ".join([str(m) for m in logdict["message"]])
prompt = '.>'
if logdict['isError'] or 'error' in text.lower():
prompt = "!>"
print prompt, text
log.addObserver(twisted_logger)
protocol.ClientCreator(reactor, SimpleTransport).connectTCP(HOST, 22)
reactor.run()
Sorry if you already tried to help me last time, seems like I didn't get it...
Best regards,
Henning Hraban Ramm
Südkurier Medienhaus / MediaPro
Support/Admin/Development Dept.
More information about the Twisted-Python
mailing list