【问题标题】:How to pass "" as argument to subprocess.check_call?如何将“”作为参数传递给 subprocess.check_call?
【发布时间】:2019-07-20 17:04:57
【问题描述】:

应用解决方案时

ssh-keygen -t rsa -N "" -f my.key

Automating "enter" keypresses for bash script generating ssh keys 在 Python 3 中我偶然发现了以下问题:

sp.check_call(["ssh-keygen", "-t", "rsa", "-N", "\"\"", "-f", "my.key"])

还有

sp.check_call(["ssh-keygen", "-t", "rsa", "-N", "''", "-f", "my.key"])

由于

而失败
Saving key "my.key" failed: passphrase is too short (minimum five characters)

sp.check_call(["ssh-keygen", "-t", "rsa", "-N", "", "-f", "my.key"])

导致ssh-keygen 提示输入应通过传递-N "" 避免的键。

实现接收-N "" 的命令的pythonic 方式是什么?我知道将命令和参数作为一个字符串传递的可能性,这可能会解决这个问题,或者从参考问题的答案中采取另一种方法。我想扩展我的 Python 知识。

【问题讨论】:

  • 最后一个应该是正确的。
  • "" 是与您在命令行中所做的相同的选项。如果这产生的结果与在命令行上编写 -N "" 不同,那么还有其他事情发生。也许你在不应该的时候通过了shell=True,或者ssh-keygen 在没有连接到 tty 时表现不同。
  • 无法在 macOS 10.14 中重现;你用的是什么操作系统?
  • 您似乎手动重新键入了这些行(例如,有几个缺少引号会产生 SyntaxError 而不是您描述的结果)。我们需要查看您实际运行过的东西,以及运行时实际得到的结果。它不一定是导致您提出这个问题的完整的原始程序;只要您运行您发布的内容,您就可以将其剥离。
  • @user2357112 你是对的,但是缺少的右引号来自用"ssh-keygen" 替换变量。无论如何,我想通了 - 不是很满意,但仍然清晰。

标签: python command-line ssh-keygen


【解决方案1】:
sp.check_call(["ssh-keygen", "-t", "rsa", "-N", "", "-f", "my.key"])

在命令行和 Docker 映像 python:3 中都可以使用,但在使用 Docker 映像 google/cloud-sdk 内的 python3 script.py 调用的脚本中调用代码时则不行。

这很有趣,但如果 tty 像某些 cmets 中所述的那样丢失,则可能是由不同的行为引起的。

【讨论】:

    猜你喜欢
    • 2019-03-21
    • 2011-09-17
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 2010-09-17
    • 1970-01-01
    相关资源
    最近更新 更多