Python: execute scp, stdin for password not working

I am trying to do the following

from subprocess import Popen, PIPE Popen(["scp", "-B"," user@url :file", "."], stdin=PIPE, shell=False).communicate(input="password") 

But I still get the promt password, and the password is not sent. I know that I can use scp with keys, but this is not what I need.

Any help?

+1
source share
2 answers

scp interacts directly with the terminal, and does not read from STDIN . You cannot pass the password over the channel, this is a security issue for scp, and it is the same for sftp, ssh.

you can try in this terminal (and this will not work):

 echo "password" | scp me@localhost :test . 
+3
source

As others have said, scp (and ssh) read the password directly from the console instead of stdin, so using a subprocess to send a password will not work. You can use pexpect - see Docs and numerous examples of how to do this.

+1
source

Source: https://habr.com/ru/post/889714/


All Articles