[Twisted-Python] Execute interactive commands with a custom ssh shell

Jean-Paul Calderone exarkun at divmod.com
Sat Aug 16 08:14:41 EDT 2008

On Sat, 16 Aug 2008 14:04:22 +0200, The Noob <thenoob06 at gmail.com> wrote:
>Hello all,
>I try to implement a custom ssh server with twisted . I use* sshserver.py to
>begin (* http://www.devshed.com/c/a/Python/SSH-with-Twisted/ )
>I would like to execute interactive scripts which are on my server. The
>problem is theses scripts are interactives. How can I redirect IO on my
>shell ?
>For example, if I want to install a software with apt. I have questions to
>install dependencies ( yes/no)
>I start to create a definition for this function like def do_apt. And then
>when in my custom shell I execute ( apt python)., that executes apt-get
>install python on the server. But how can I redirect the output in my shell
>to answer dependencies questions ?
>Thanks for your help

To run a program like apt, you must be using `reactor.spawnProcess´.  That
means that you have two different protocol implementations.  One to talk
to the SSH connection (eg, `SSHDemoProtocol´ in the O'Reilly example) and
one to talk to the child process.

If you want output from the child process to show up in the SSH client, then
when the process protocol receives data from it, it should pass it on to the
SSH protocol to write to the client.  Similarly, if you want input from the
SSH client written to the stdin of the child process, when the SSH protocol
receives data it should pass it on to the process protocol to write to the
child process.

You'll have to keep track of what state your application is in to do this
correctly.  Input should only be handled this way when there is actually
a child process running.


More information about the Twisted-Python mailing list