[Twisted-Python] question about twisted conch (SSH) and wxPython

Astan Chee stanc at al.com.au
Mon Jul 18 03:02:52 EDT 2005


Whoops,
I forgot to add the wantReply=1 there...
anyway, Another question I have is if the password is incorrect, quit 
the python script...

currently the getPassword function looks like this
RETRIES = 0
def getPassword(self):
        global RETRIES
        if RETRIES  == 0:
            RETRIES = 1
            return defer.succeed(PASSWORD)
        else:           
            sys.exit(0)

But it keeps on giving this error:
Traceback (most recent call last):
  File "c:\Python24\lib\site-packages\twisted\python\context.py", line 
31, in ca
llWithContext
    return func(*args,**kw)
  File 
"c:\Python24\lib\site-packages\twisted\internet\selectreactor.py", line 1
39, in _doReadOrWrite
    why = getattr(selectable, method)()
  File "c:\Python24\lib\site-packages\twisted\internet\tcp.py", line 
351, in doR
ead
    return self.protocol.dataReceived(data)
  File "c:\Python24\Lib\site-packages\twisted\conch\ssh\transport.py", 
line 189,
 in dataReceived
    ord(packet[0]), packet[1:])
--- <exception caught here> ---
  File "c:\Python24\lib\site-packages\twisted\python\log.py", line 56, 
in callWi
thLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "c:\Python24\lib\site-packages\twisted\python\log.py", line 41, 
in callWi
thContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "c:\Python24\lib\site-packages\twisted\python\context.py", line 
52, in ca
llWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "c:\Python24\lib\site-packages\twisted\python\context.py", line 
31, in ca
llWithContext
    return func(*args,**kw)
  File "c:\Python24\Lib\site-packages\twisted\conch\ssh\service.py", 
line 42, in
 packetReceived
    f(packet)
  File "c:\Python24\Lib\site-packages\twisted\conch\ssh\userauth.py", 
line 259,
in ssh_USERAUTH_FAILURE
    if method not in self.authenticatedWith and self.tryAuth(method):
  File "c:\Python24\Lib\site-packages\twisted\conch\ssh\userauth.py", 
line 231,
in tryAuth
    return f()
  File "c:\Python24\Lib\site-packages\twisted\conch\ssh\userauth.py", 
line 344,
in auth_password
    d = self.getPassword()
  File "D:\stanc_vault\dev\WHIP\whip.py", line 67, in getPassword
    sys.exit(1)
exceptions.SystemExit: 1

Astan Chee wrote:

> Hi Guys,
> Im trying to make a psudo SSH client GUI, but it seems that I although 
> I can 'ls' to a remote machine using twisted.conch, I cant `ls -al`. 
> Also I cant seem to to ssh commands that have spaces in them. Although 
> Im using a windows environment to SSH, i dont think that is the problem.
> Here is a snippet of my code. Obviously I've already called 
> reactor.run() and included appropriate modules and variables. I dont 
> understand why ls works and ls -al doesnt. Also I was wondering if 
> there are any existing command line SSH clients written with twisted 
> similar to putty, but written in python.
> Thanks!
>
> 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(PASSWORD)
>
>    def getGenericAnswers(self, name, instruction, questions):
>        print 'Name %s' % name
>        print 'Instruction %s' % 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):
>        path = os.path.expanduser('~/.ssh/id_dsa')
>        # this works with rsa too
>        # just change the name here and in getPrivateKey
>        if not os.path.exists(path) or self.lastPublicKey:
>            # the file doesn't exist, or we've tried a public key
>            return
>        return keys.getPublicKeyString(path+'.pub')
>
>    def getPrivateKey(self):
>        path = os.path.expanduser('~/.ssh/id_dsa')
>        return defer.succeed(keys.getPrivateKeyObject(path))
>
> class SimpleConnection(connection.SSHConnection):
>    def serviceStarted(self):
>        self.openChannel(TrueChannel(2**16, 2**15, self))
>        self.openChannel(FalseChannel(2**16, 2**15, self))
>        self.openChannel(LsChannel(2**16, 2**15, self))
>
> class TrueChannel(channel.SSHChannel):
>    name = 'session' # needed for commands
>
>    def openFailed(self, reason):
>        print 'true failed', reason
>      def channelOpen(self, ignoredData):
>        self.conn.sendRequest(self, 'exec', common.NS('true'))
>
>    def request_exit_status(self, data):
>        status = struct.unpack('>L', data)[0]
>        #print 'true status was: %s' % status
>        self.loseConnection()
>
> class FalseChannel(channel.SSHChannel):
>    name = 'session'
>
>    def openFailed(self, reason):
>        print 'false failed', reason
>
>    def channelOpen(self, ignoredData):
>        self.conn.sendRequest(self, 'exec', common.NS('false'))
>
>    def request_exit_status(self, data):
>        status = struct.unpack('>L', data)[0]
>        #print 'false status was: %s' % status
>        self.loseConnection()
>       class LsChannel(channel.SSHChannel):
>    name = 'session'
>
>    def openFailed(self, reason):
>        print 'false failed', reason
>
>    def channelOpen(self, ignoredData):
>        self.conn.sendRequest(self, 'exec', common.NS('ls -al'))
>        self.data = ''
>
>    def dataReceived(self, data):
>        self.data += data
>
>    def closed(self):
>        print 'got data from ls: %s' % repr(self.data)
>        self.loseConnection()
>        reactor.stop()
>       class CatChannel(channel.SSHChannel):
>    name = 'session'
>
>    def openFailed(self, reason):
>        print 'echo failed', reason
>
>    def channelOpen(self, ignoredData):
>        self.data = ''
>        d = self.conn.sendRequest(self, 'exec', common.NS('cat'), 
> wantReply = 1)
>        d.addCallback(self._cbRequest)
>
>    def _cbRequest(self, ignored):
>        self.write('hello conch\n')
>        self.conn.sendEOF(self)
>
>    def dataReceived(self, data):
>        self.data += data
>
>    def closed(self):
>        print 'got data from cat: %s' % repr(self.data)
>        self.loseConnection()
>
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>




More information about the Twisted-Python mailing list