【发布时间】:2014-04-23 21:39:04
【问题描述】:
我正在尝试自动设置生成自签名 SSL 证书。这是我的代码:
#!/usr/bin/env python
import subprocess
pass_phrase = 'example'
common_name = 'example.com'
webmaster_email = 'webmaster@example.com'
proc = subprocess.Popen(['openssl', 'req', '-x509', '-newkey', 'rsa:2048', '-rand', '/dev/urandom', '-keyout', '/etc/pki/tls/private/server.key', '-out', '/etc/pki/tls/certs/server.crt', '-days', '180'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
for i in range(2):
proc.stdin.write(pass_phrase)
for i in range(5):
proc.stdin.write('.')
proc.stdin.write(common_name)
proc.stdin.write(webmaster_email)
proc.stdin.flush()
stdout, stderr = proc.communicate()
当我运行它时,它仍然提示我输入 PEM 密码,然后返回此错误:
Country Name (2 letter code) [XX]:weird input :-(
problems making Certificate Request
它应该输入上面的密码,而不是提示我输入任何内容。有什么想法吗?
PS。我知道pexpect。请不要向我建议。
编辑:经过进一步调查,我已经弄清楚了。如果不指定 -nodes,私钥将被加密。因此,OpenSSL 将立即提示输入 PEM 密码。这意味着我的 stdin.write() 的顺序搞砸了。我想另一种方法是使用 -nodes 并稍后加密私钥。
【问题讨论】:
-
它看起来像是在寻找一个国家名称,但你没有给它。
-
为什么:“我知道。请不要向我推荐它。”?
-
因为根据我在 stackoverflow 上的经验,用户倾向于用一般的回答来回复,希望这会为他们赢得积分,而不是真诚地回答问题。
标签: python subprocess