Problems writting a wrapper around linux' ssh command

Dan Maas dmaas at dcine.com
Sat Mar 10 15:34:45 EST 2001


> I am trying to write a python wrapper around ssh. The
> last and only problem I have is password verification.
> I try to popen2.popen3 the ssh command, but no matter
> what I write to the input file, the ssh command ignores it
> and keeps requesting *keyboard* input.

Right, ssh tries to bypass stdin and read the password from its controlling
terminal. You can avoid this by creating a virtual terminal and attaching to
it before executing ssh. The standard 'pty' modules does it for you:

import pty, os

[ssh_pid, ssh_fd] = pty.fork()

if ssh_pid == 0: # child
     os.execvp('ssh', ['ssh', hostname])

... and now you can talk to the ssh process via ssh_fd.

Be aware that the pty module is somewhat of a hack - the process of setting
up a virtual terminal is very platform-dependent, and Linux 2.4 introduced a
new method that pty doesn't use yet. It should work fine on most Linux
systems though.

Regards,
Dan





More information about the Python-list mailing list