[Twisted-Python] Conch channel blocked

Hello, I'm trying to get a feel for twisted.conch, and not getting very far. I know the documentation is lacking, but I'm not here to complain, I'm here to figure this out. I only have a cursory knowledge of twisted so far, so that may be some of my problem. I am however fairly familiar with the internals of ssh, so I thought I could start there... I started with the sshsimpleclient example, took some pieces, and began experimenting. The example worked as expected. Next, I wanted to pipe arbitrary data over the channel, as if I called "ssh hostname 'cat > file'" This however fills the remote buffer, and hangs Here's a small example ############### class ClientConnection(connection.SSHConnection): def serviceStarted(self): self.openChannel(CatChannel(conn = self)) class CatChannel(channel.SSHChannel): name = 'session' def channelOpen(self, data): d = self.conn.sendRequest(self, 'exec', common.NS('cat > /tmp/testfile'), wantReply = 1) d.addCallback(self._cbSendRequest) def _cbSendRequest(self, ignored): f = open('testfile') chunk = f.read(4096) while chunk: self.write(chunk) chunk = f.read(4096) f.close() self.conn.sendEOF(self) self.loseConnection() ################ Also, setting the channel name to 'session', sending the 'exec' request with the command as a netstring, etc. seem like something that should be abstracted away. SSHSession with request_exec seemed like the obvious choice, but I had no success in using that class for anything. Does anyone know if any projects making good use of twisted.conch, where I can see the source in action, and in depth? Thanks, -jim

On 2 Feb, 09:24 pm, jbardin@bu.edu wrote:
Hello,
I'm trying to get a feel for twisted.conch, and not getting very far. I know the documentation is lacking, but I'm not here to complain, I'm here to figure this out. I only have a cursory knowledge of twisted so far, so that may be some of my problem. I am however fairly familiar with the internals of ssh, so I thought I could start there...
I started with the sshsimpleclient example, took some pieces, and began experimenting. The example worked as expected. Next, I wanted to pipe arbitrary data over the channel, as if I called "ssh hostname 'cat > file'" This however fills the remote buffer, and hangs
Here's a small example ############### class ClientConnection(connection.SSHConnection): def serviceStarted(self): self.openChannel(CatChannel(conn = self))
class CatChannel(channel.SSHChannel): name = 'session' def channelOpen(self, data): d = self.conn.sendRequest(self, 'exec', common.NS('cat > /tmp/testfile'), wantReply = 1) d.addCallback(self._cbSendRequest)
def _cbSendRequest(self, ignored): f = open('testfile') chunk = f.read(4096) while chunk: self.write(chunk) chunk = f.read(4096) f.close() self.conn.sendEOF(self) self.loseConnection() ################
Also, setting the channel name to 'session', sending the 'exec' request with the command as a netstring, etc. seem like something that should be abstracted away. SSHSession with request_exec seemed like the obvious choice, but I had no success in using that class for anything.
I can't run your example code, since it's not a complete example. That makes it harder to point out what's wrong with it. However, one thing does strike me as wrong:
while chunk: self.write(chunk) chunk = f.read(4096)
This is a pattern you typically want to avoid, in Conch or anything else Twisted-based. I don't know if SSHChannel is a consumer, but if not it should be, and you should be writing the file using a producer. See: http://twistedmatrix.com/documents/current/core/howto/producers.html
Does anyone know if any projects making good use of twisted.conch, where I can see the source in action, and in depth?
Mantissa uses Twisted Conch a bit: http://divmod.org/trac/browser/trunk/Mantissa/xmantissa/terminal.py But you won't find any code sending any exec requests there. Jean-Paul

On Thu, Feb 4, 2010 at 9:01 AM, <exarkun@twistedmatrix.com> wrote:
I can't run your example code, since it's not a complete example. That makes it harder to point out what's wrong with it. However, one thing does strike me as wrong:
while chunk: self.write(chunk) chunk = f.read(4096)
This is a pattern you typically want to avoid, in Conch or anything else Twisted-based. I don't know if SSHChannel is a consumer, but if not it should be, and you should be writing the file using a producer. See:
It looked awkward, but the one example in the documentation was my only starting point. http://twistedmatrix.com/documents/current/conch/howto/conch_client.html This is only a slightly modified version of that example, which writes directly through the session channel, which makes sense in the ssh-protocol, but apparently not here.
http://twistedmatrix.com/documents/current/core/howto/producers.html
Thanks. I'll see if I can relate that into conch.
Does anyone know if any projects making good use of twisted.conch, where I can see the source in action, and in depth?
Mantissa uses Twisted Conch a bit:
http://divmod.org/trac/browser/trunk/Mantissa/xmantissa/terminal.py
But you won't find any code sending any exec requests there.
That's OK. any little bit helps. I have a twisted service, and it needs to tunnel some information through an ssh channel. Although this would take me a couple minutes with another library, I feel I should be able to make use of what's already built in to twisted, and not add another dependency. -jim

2010/2/4 james bardin <jbardin@bu.edu>:
On Thu, Feb 4, 2010 at 9:01 AM, <exarkun@twistedmatrix.com> wrote:
I can't run your example code, since it's not a complete example. That makes it harder to point out what's wrong with it. However, one thing does strike me as wrong:
while chunk: self.write(chunk) chunk = f.read(4096)
This is a pattern you typically want to avoid, in Conch or anything else Twisted-based. I don't know if SSHChannel is a consumer, but if not it should be, and you should be writing the file using a producer. See:
It looked awkward, but the one example in the documentation was my only starting point. http://twistedmatrix.com/documents/current/conch/howto/conch_client.html
This is only a slightly modified version of that example, which writes directly through the session channel, which makes sense in the ssh-protocol, but apparently not here.
http://twistedmatrix.com/documents/current/core/howto/producers.html
Thanks. I'll see if I can relate that into conch.
Does anyone know if any projects making good use of twisted.conch, where I can see the source in action, and in depth?
Mantissa uses Twisted Conch a bit:
http://divmod.org/trac/browser/trunk/Mantissa/xmantissa/terminal.py
But you won't find any code sending any exec requests there.
That's OK. any little bit helps. I have a twisted service, and it needs to tunnel some information through an ssh channel. Although this would take me a couple minutes with another library, I feel I should be able to make use of what's already built in to twisted, and not add another dependency. Maybe you can use connection forwarding in conch? Have a look at http://twistedmatrix.com/trac/browser/trunk/twisted/conch/scripts/conch.py to see how the forwarding API works.
Kuba
-jim
participants (3)
-
exarkun@twistedmatrix.com
-
james bardin
-
Kuba Konczyk