Redirecting stdout and stdin

Donn Cave donn at u.washington.edu
Thu Mar 4 21:42:00 CET 2004


In article <c281ap$2l5$1 at coward.ks.cc.utah.edu>,
 Matthew Thorley <ruach at chpc.utah.edu> wrote:
> I am working on a script that calls a program ssh in this case. The 
> script needs to be non-interactive so I need python to comunicate with 
> ssh. I am having difficult time opening a pipe to the program and 
> reading and writing output to it. I tried using popen something like:
> 
> ssh = popen('ssh user at host')
> if ssh[1].readline = 'something':
> 	ssh[0].write('password')
> 
> Etc...
> 
> 
> But everytime I run the thing stdout still shows up on the command line 
> and I get propmted to enter the password interactively.

As suggested in another followup, you may need some type of
"expect" interface to deal with ssh's password prompt, because
ssh needs a tty device there.  It turns off tty echo, and may
not be willing to proceed if the (pipe) device can't handle that
ioctl.  It also needs a distinct source for the password input,
because it's supposed to read up stdin input and copy it to the
remote sshd, so it will open /dev/tty for the password.

If this sounds like a lot of trouble, it's really worse than it
sounds.  Ssh can authenticate other ways besides passwords, and
that's really worth considering.  In that case you can use plain
popen (instead of popen2 which you evidently were using above),
and stdout will not show up on the terminal (as it wasn't in
your experiment above - that was stderr.)

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list