Hi All,

A day back I install Twisted and Conch on RHEL5. I am a novice user of
Twisted software.
I copied an example from dev-shed website (attached herewith). This example
script uses conch to talk to a ssh deamon.

This script was able to capture and return the output of simple commands
such as - ls, cat etc.
I mean, when I run the script to execute 'ls -al' on a remote machine it
does and returns the prompt.

Where as, when I run the script to execute '*tail -f /path/to/mylog.txt*' I
get the ouput of this command but the prompt never returns.
When I press ^C (Control-C) I get the following run-time error.

*Traceback (most recent call last):
  File
"/usr/lib/python2.4/site-packages/Twisted-8.2.0-py2.4-linux-i686.egg/twisted/internet/tcp.py",
line 520, in connectionLost
    protocol.connectionLost(reason)
  File
"/usr/lib/python2.4/site-packages/Twisted-8.2.0-py2.4-linux-i686.egg/twisted/conch/ssh/transport.py",
line 166, in connectionLost
    self.service.serviceStopped()
  File
"/usr/lib/python2.4/site-packages/Twisted-8.2.0-py2.4-linux-i686.egg/twisted/conch/ssh/connection.py",
line 59, in serviceStopped
    map(self.channelClosed, self.channels.values())
  File
"/usr/lib/python2.4/site-packages/Twisted-8.2.0-py2.4-linux-i686.egg/twisted/conch/ssh/connection.py",
line 580, in channelClosed
    log.callWithLogger(channel, channel.closed)
--- <exception caught here> ---
  File
"/usr/lib/python2.4/site-packages/Twisted-8.2.0-py2.4-linux-i686.egg/twisted/python/log.py",
line 84, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File
"/usr/lib/python2.4/site-packages/Twisted-8.2.0-py2.4-linux-i686.egg/twisted/python/log.py",
line 69, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File
"/usr/lib/python2.4/site-packages/Twisted-8.2.0-py2.4-linux-i686.egg/twisted/python/context.py",
line 59, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File
"/usr/lib/python2.4/site-packages/Twisted-8.2.0-py2.4-linux-i686.egg/twisted/python/context.py",
line 37, in callWithContext
    return func(*args,**kw)
  File "shankha.py", line 56, in closed
    reactor.stop()
  File
"/usr/lib/python2.4/site-packages/Twisted-8.2.0-py2.4-linux-i686.egg/twisted/internet/base.py",
line 526, in stop
    raise error.ReactorNotRunning(
twisted.internet.error.ReactorNotRunning: Can't stop reactor that isn't
running.*


*I know that 'tail -f' command is expecting "Control-C" or Break to return
the prompt on remote host.
But my attempt to send the ^C character is failing..

I am doing somethink like this:

python sshc.py <hostname> 'tail -f mylog.txt ; \x03'

What is the recommended method to adopt such cases?

*Cheers!!
Harsha Reddy
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

    def verifyHostKey(self, pubKey, fingerprint):
        # in a real app, you should verify that the fingerprint matches
        # the one you expected to get from this server
        return defer.succeed(True)

    def connectionSecure(self):
        self.requestService(
            PasswordAuth(self.username, self.password,
                         ClientConnection(self.command)))

class PasswordAuth(userauth.SSHUserAuthClient):
    def __init__(self, user, password, connection):
        userauth.SSHUserAuthClient.__init__(self, user, connection)
        self.password = password

    def getPassword(self, prompt=None):
        return defer.succeed(self.password)

class ClientConnection(connection.SSHConnection):
    def __init__(self, cmd, *args, **kwargs):
        connection.SSHConnection.__init__(self)
        self.command = cmd

    def serviceStarted(self):
        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):
        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):
        reactor.stop()

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() 
_______________________________________________
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

Reply via email to