使用子进程通过SSH或SCP发送密码 [英] Sending a password over SSH or SCP with subprocess.Popen

查看:160
本文介绍了使用子进程通过SSH或SCP发送密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行> 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()

这立即返回错误:

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

我确定密码正确.我可以通过在外壳上手动调用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(这通常是与需要输入的命令行程序进行交互的正确方法).有一个叉子,它可用于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.

这篇关于使用子进程通过SSH或SCP发送密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆