Channel72 Channel72 - 3 years ago 307
Linux Question

Sending a password over SSH or SCP with subprocess.Popen

I'm trying to run an

(secure copy) command using
. The login requires that I send a password:

from subprocess import Popen, PIPE

proc = Popen(['scp', "user@", "."], stdin = PIPE)

This immediately returns an error:

user@'s password:
Permission denied, please try again.

I'm certain the password is correct. I easily verify it by manually invoking
on the shell. So why doesn't this work?

Note, there are many similar questions to this, asking about
and sending a password for automated SSH or FTP login:

How can I set a users password in linux from a python script?

Use subprocess to send a password

The answer(s) to these questions don't work and/or don't apply because I am using Python 3.

Answer Source

The second answer you linked suggests you use Pexpect(which is usually the right way to go about interacting with command line programs that expect input). There is a fork of it which works for python3 which you can use.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download