使用 subprocess.Popen 通过 SSH 或 SCP 发送密码 [英] Sending a password over SSH or SCP with subprocess.Popen
问题描述
我正在尝试运行 scp
(安全复制)命令使用 subprocess.Popen
.登录要求我发送密码:
I'm trying to run an scp
(secure copy) command using subprocess.Popen
. The login requires that I send a password:
from subprocess import Popen, PIPE
proc = Popen(['scp', "user@10.0.1.12:/foo/bar/somefile.txt", "."], stdin = PIPE)
proc.stdin.write(b'mypassword')
proc.stdin.flush()
这会立即返回一个错误:
This immediately returns an error:
user@10.0.1.12's password:
Permission denied, please try again.
我确定密码是正确的.我通过在 shell 上手动调用 scp
来轻松验证它.那么为什么这不起作用?
I'm certain the password is correct. I easily verify it by manually invoking scp
on the shell. So why doesn't this work?
注意,有很多与此类似的问题,询问subprocess.Popen
并发送密码以进行自动 SSH 或 FTP 登录:
Note, there are many similar questions to this, asking about subprocess.Popen
and sending a password for automated SSH or FTP login:
我该怎么做从 python 脚本在 linux 中设置用户密码?
使用子进程发送密码
这些问题的答案不起作用和/或不适用,因为我使用的是 Python 3.
The answer(s) to these questions don't work and/or don't apply because I am using Python 3.
推荐答案
您链接的第二个答案建议您使用 Pexpect(这通常是与需要输入的命令行程序进行交互的正确方法).有一个 fork 它适用于您可以使用的 python3.
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.
这篇关于使用 subprocess.Popen 通过 SSH 或 SCP 发送密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!