Python:执行 scp、stdin 密码不起作用

发布于 2024-10-01 12:40:16 字数 247 浏览 0 评论 0原文

我正在尝试以下操作

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

,但我仍然收到密码提示,并且没有发送密码。 我知道我可以使用 scp 和密钥,但这不是我需要的。

有什么帮助吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小…红帽 2024-10-08 12:40:16

scp 直接与终端交互,而不是从 STDIN 读取,您无法通过管道传递密码,这对于 scp 来说是安全问题,对于 sftp 来说也是如此, SSH。

你可以像这样在终端中尝试(它不会工作):

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

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 .
谁的年少不轻狂 2024-10-08 12:40:16

正如其他人所说,scp(和ssh)直接从控制台而不是stdin读取密码,因此使用子进程发送密码将不起作用。您可以使用 pexpect 代替 - 请参阅文档和大量示例以了解如何执行此操作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文