[Twisted-Python] how to get extReceived call from SSHCommandClientEndpoint

Glyph glyph at twistedmatrix.com
Sun Apr 7 01:15:41 MDT 2019



> On Apr 6, 2019, at 10:12 AM, Sereysethy TOUCH <touch.sereysethy at gmail.com> wrote:
> 
> Hi Jean-Paul,
> 
> No I dont want it to be mixed with stdout stream, it should be separated from normal stdout, so that I know it is an error.
> 
> To be honest, I am pretty to new to Twisted concept, there are things that I have not familiar with yet. Actually I can also implement a low level ssh client (which I already did), but as SSHCommandClientEndpoint is already available, so I want to use it straight away. But when I dived in the source code, the _CommandChannel only implements dataReceived but not extReceived.

The main point of `SSHCommandClientEndpoint` is to allow code that doesn't know about SSH to run over SSH, for example to enable forwarding.  So you can't unconditionally require everyone to implement extReceived, because IProtocol doesn't require this.

> For your suggestion on using ProcessEndpoint how can I use it for my SSH client?

Jean-Paul was suggesting a way that Twisted could be modified to support your use-case, not a thing that you can do today in your own code.

> In my extReceived I just deliver what I got to the extReceived of method protocol, it is exactly the same as Twisted implements dataReceived:
> 
> class CommandChannel(_CommandChannel): 
>     def extReceived(self, dataType, data):
>         """
>         When the command's extended data (usually standard error) arrives,
>         deliver it to the protocol instance.
> 
>         @type dataType: L{int}
>         @type data:     L{str}
>         """
>         self._protocol.extReceived(dataType, data)

If you wanted to contribute this functionality to Twisted, the usual idiom here would be to do a check, like:

protocol = self._protocol
if ISSHExtendedProtocol.providedBy(protocol):
    protocol.extReceived(dataType, data)

Doing this on every extReceived may be a little bit of a performance hit, so caching this computation and/or the behavior associated with it is left as an exercise for you :).

> I have two other questions since you are here
> 
> 1) how can we execute command like more or less which is an interactive command?

Looking at what the 'conch' script does, I think what you're asking is something like this:

self.conn.sendRequest(self, b'exec', common.NS(options['command']))

> 2) how to request a pty to ssh server?

Again from twisted/conch/scripts/conch.py:

ptyReqData = session.packRequest_pty_req(term, winSize, '')
self.conn.sendRequest(self, b'pty-req', ptyReqData)


> 
> Thanks,
> Sethy

Thanks for using Twisted,

-g

-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20190407/f968d53b/attachment-0002.html>


More information about the Twisted-Python mailing list