我正在尝试自动设置生成自签名的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密码,然后返回以下错误:
^{pr2}$它应该输入上面的密码,而不是提示我任何东西。有什么问题吗?在
另外,我知道pexpect。请不要向我建议。在
编辑:经过进一步调查,我已经弄明白了。如果不指定-nodes,私钥将被加密。因此,OpenSSL将立即提示输入PEM密码短语。这意味着我的命令标准写入()搞砸了。我想另一种选择是使用-nodes并稍后加密私钥。在
您的代码中有几个错误,例如,没有新行发送到子进程。在
主要问题是
openssl
直接从终端获得密码短语(就像Python中的getpass.getpass()
)。请参见Why not just use a pipe (popen())?中的第一个原因:提供伪tty的
pexpect
在这种情况下可以正常工作:另一种方法是尝试使用
-passin
选项来指示openssl
从不同的源(stdin、文件、管道、envvar、命令行)获取密码短语。我不知道它是否与openssl req
命令一起工作。在两个问题:
write()
方法不会自动插入新行。您需要将"\n"
添加到字符串中,或者在要提供给程序的每一行输入之后write()
分开{proc.stdin.write(pass_phrase + "\n")
相关问题 更多 >
编程相关推荐