Help with Popen3 objects and ssh... Python 2.x

Andrew MacIntyre andymac at bullseye.apana.org.au
Thu Apr 19 07:30:39 EDT 2001


On Wed, 18 Apr 2001, Gilles Lenfant wrote:

> I need to run and get the result of a command on a remote server through an
> ssh client.
> I can't find the way to send the password to the server.
> But I'm always asked interactively for the password through the console
> despite I write the "sh.tochild.write(passwd)"
>
> Please help !
>
> server = 'myserver'
> user = 'myself'
> passwd = 'mysecret'
> command = 'ls -la'
>
> import popen2
> def rmtCmd(acmd):
>     ''''builds a string suited to execute a command through ssh'''
>     global user, server
>     return 'ssh -l %s %s %s' % (user, server, acmd)
>
> sh = popen2.Popen3('')
>
> if __name__ == '__main__':
>     # Tests stuffs
>     rcmd = rmtCmd(command)
>     sh = popen2.Popen3(rcmd)
>     sh.tochild.write(passwd)
>     sh.wait()
>     print sh.fromchild.read()
>
> I've tried other combinations to get it working... desperately !

A question and a comment...

Why are you using the popen2.Popen3 class, when popen2.popen3() would be
much more portable?

I would be suggesting that you add a '\n' to your sh.tochild.write()
- which is probably the real cause of your problem.

ie:
    sh.tochild.write('%s\n' % passwd)

--
Andrew I MacIntyre                     "These thoughts are mine alone..."
E-mail: andrew.macintyre at aba.gov.au    (work) | Snail: PO Box 370
        andymac at bullseye.apana.org.au  (play) |        Belconnen  ACT  2616
        andymac at pcug.org.au           (play2) |        Australia





More information about the Python-list mailing list