开发者

Python: Execute scp, stdin for password not working

开发者 https://www.devze.com 2023-01-24 14:34 出处:网络
I\'m trying the following from subprocess import Popen, PIPE Popen([\"scp\", \"-B\"开发者_如何学编程,\"user@url:file\", \".\"], stdin=PIPE, shell=False).communicate(input=\"password\")

I'm trying 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 password promt, and no password is sent. I know I can use scp with keys, but this is not what I need.

Any help?


scp interacts with the terminal directly, rather than reading from STDIN, You can't pass the password via a pipe, it security matter for scp and it's the same for sftp, ssh.

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

echo "password" | scp me@localhost:test .


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

0

精彩评论

暂无评论...
验证码 换一张
取 消