[Twisted-Python] Executing a number of commands over SSH
exarkun at divmod.com
Sat Jun 14 21:43:51 EDT 2008
On Sat, 14 Jun 2008 16:08:58 -0700, James Brady <james at webmynd.com> wrote:
>I'd like to run a few commands on a remote machine over SSH. Looking at the
>conch packages, I'm not sure what the best approach is for this.
>The only way I can see to pass the set of commands down into the
>SSHConnection and SSHChannel objects is through my transport factory, i.e.
>I can't add or remove commands to be run on an already instantiated
It sounds like you've figured out a lot of the difficult parts already. :)
Consider how you execute a command once `SSHConnection.serviceStarted´ is
called (if you looked at `sshsimpleclient.py´, then this is where the True,
False, and Cat channels get opened). All you need to do to execute another
command is what you would do to execute those initial commands in that
method. As long as you have a reference to the SSHConnection instance, you
can start arbitrary commands at any time you like.
So the problem is really just figuring out how to have a reference to the
SSHConnection instance in the code that knows it wants to start a new
>Also, I'm a newcomer to the Twisted way of doing things, so it's not clear
>to me how I would go about returning the status and output of these
>commands back up to my code. I'm guessing I should create a Deferred
>somewhere, but some guidance on how these should bubble up through the
>twisted layers would be much appreciated.
Indeed, a Deferred should help. You can have one which is fired with the
SSHConnection once it is available. Then, callbacks on this Deferred will
be able to start any commands they want. One way to do this would be to
pass a Deferred all the way down into the SSHConnection and fire it with
`self´ in `serviceStarted´. This basically turns control flow inside-out,
allowing arbitrary code which is not part of the SSHConnection class to use
This has been a bit vague, but Conch certainly has a lot of pieces and it's
hard to talk more specifically without having some code at hand. If you're
still stuck, I suggest posting a short example of what you have (preferably
self-contained and runnable) and then someone can make more specific code
suggestions based on it.
More information about the Twisted-Python