Python 子进程 + scp - 无法读取所有输出
我正在尝试在计算机之间发送 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“无法确定主机 '***' 的真实性”
表示您连接的计算机尚未被告知将另一端(服务器)身份保存到known_hosts< /code> 文件,它询问您是否信任该机器。您可以将 ssh 客户端更改为自动添加,而不提示您。
试试这个:
使用上面的代码我得到:
如果我使用禁用
StrictHostKeyChecking
我得到:所以它看起来像是在打开
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 theknown_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:
With the above code i get:
If I use disable
StrictHostKeyChecking
i get:So it looks like it is printing the first line from stderr with
BatchMode
turned on :)我以前遇到过类似的事情,尽管对我来说它实际上很有帮助。我相信 ssh 和朋友实际上并不读取 stdin 并在 stdout 或 stderr 上打印,他们会做一些时髦的事情来直接连接到您正在运行的终端。
我相信原因是他们应该能够直接与用户对话,即使通过包装 shell 脚本运行也是如此,因为用户知道密码,而不是调用脚本(并且可能他们故意不希望调用脚本)有机会拦截密码)。
[编辑添加]:根据我系统上的手册页, scp 确实有一个标志可以执行您想要的操作:
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: