Python 子进程 + scp - 无法读取所有输出

发布于 2024-11-14 10:41:46 字数 714 浏览 3 评论 0原文

我正在尝试在计算机之间发送 SCP 文件,当用户未设置私有/公共证书来进行无密码登录时,我需要失败。不幸的是,使用 subprocess.Popen 我无法弄清楚如何捕获以下输出:

The authenticity of host '***' can't be established.
RSA key fingerprint is ***.
Are you sure you want to continue connecting (yes/no)

它总是显示在控制台上,并且我无法在程序中检测到它。

这是一些示例代码:

proc = subprocess.Popen(['scp', 'user@server:/location/file.txt', '/someplace/file.txt',
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE)
proc.wait()
print 'result: %s' % repr(proc.stderr.readline())

我尝试了许多其他排列。这个仍然提示我,而不是 Python 输入是/否。至少当我输入 no 时,我得到:

result: 'Host key verification failed.\r\n'

I'm trying to SCP a file between machines and I need to fail when the user hasn't set up a private/public certificate to do passwordless logins. Unfortunatly, using subprocess.Popen I can't figure out how to capture the following output:

The authenticity of host '***' can't be established.
RSA key fingerprint is ***.
Are you sure you want to continue connecting (yes/no)

It always shows up on the console and I can't get it in my program to detect it.

Here's some example code:

proc = subprocess.Popen(['scp', 'user@server:/location/file.txt', '/someplace/file.txt',
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE)
proc.wait()
print 'result: %s' % repr(proc.stderr.readline())

I've tried many other permutations. This one still prompts me, and not Python to enter yes/no. At least when I type no though I get:

result: 'Host key verification failed.\r\n'

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

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

发布评论

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

评论(2

热风软妹 2024-11-21 10:41:46

“无法确定主机 '***' 的真实性” 表示您连接的计算机尚未被告知将另一端(服务器)身份保存到 known_hosts< /code> 文件,它询问您是否信任该机器。您可以将 ssh 客户端更改为自动添加,而不提示您。

试试这个:

proc = subprocess.Popen(['scp', '-o BatchMode=yes',
                                'user@server:/location/file.txt',
                                '/someplace/file.txt'],
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE)
proc.wait()
print 'result: %s' % repr(proc.stderr.readline())

使用上面的代码我得到:

me@myMachine:~$ python tmp.py 
result: 'Host key verification failed.\r\n'
me@myMachine:~$

如果我使用禁用 StrictHostKeyChecking 我得到:

me@myMachine:~$ python tmp.py
result: 'Permission denied (publickey,password,keyboard-interactive).\r\n'
me@myMachine:~$ python tmp.py

所以它看起来像是在打开 BatchMode 的情况下从 stderr 打印第一行:)

'The authenticity of host '***' can't be established' means the machine your connecting from hasn't been told to save the other ends (server) identity to the known_hosts file and it asking if you trust the machine. You can change the ssh client to just add it automatically without prompting you.

try this:

proc = subprocess.Popen(['scp', '-o BatchMode=yes',
                                'user@server:/location/file.txt',
                                '/someplace/file.txt'],
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE)
proc.wait()
print 'result: %s' % repr(proc.stderr.readline())

With the above code i get:

me@myMachine:~$ python tmp.py 
result: 'Host key verification failed.\r\n'
me@myMachine:~$

If I use disable StrictHostKeyChecking i get:

me@myMachine:~$ python tmp.py
result: 'Permission denied (publickey,password,keyboard-interactive).\r\n'
me@myMachine:~$ python tmp.py

So it looks like it is printing the first line from stderr with BatchMode turned on :)

祁梦 2024-11-21 10:41:46

我以前遇到过类似的事情,尽管对我来说它实际上很有帮助。我相信 ssh 和朋友实际上并不读取 stdin 并在 stdout 或 stderr 上打印,他们会做一些时髦的事情来直接连接到您正在运行的终端。

我相信原因是他们应该能够直接与用户对话,即使通过包装 shell 脚本运行也是如此,因为用户知道密码,而不是调用脚本(并且可能他们故意不希望调用脚本)有机会拦截密码)。

[编辑添加]:根据我系统上的手册页, scp 确实有一个标志可以执行您想要的操作:

 -B 选择批处理模式(防止要求输入密码或密码短语)。

I've run into something similar before, though in my case it was actually helpful. I believe ssh and friends don't actually read stdin and print on stdout or stderr, they do funky things to hook up with the terminal you're running in directly.

I believe the reasoning is they they're supposed to be able to talk to the user directly, even when run through wrapper shell scripts, because the user knows the password, not the calling script (and probably they deliberately don't want calling scripts to have the opportunity to intercept a password).

[Edit to add]: According to the man page on my system, scp does have a flag that might do what you want:

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