Python子进程问题
我正在编写一个脚本来用 Python 生成 CSR。脚本非常简单。我使用以下命令生成 RSA 私钥:
keycmd = "openssl genrsa -out mykey.pem 2048"
keyprocess = Popen(keycmd, shell=True, stdout=PIPE)
csrcmd = "openssl req -new -key mykey.pem -subj "+ subj + " -out mycsr.csr"
reqprocess = Popen(csrcmd, shell=True, stdout=PIPE)
但是,我想添加用户期望的使用密码加密私钥的功能。这通常是通过在 genrsa 命令中包含选项“-des3”来完成的,但我不知道如何将字符串从 Python 标准输入传输到 OpenSSL 进程。任何帮助将不胜感激。
我想做的是:
keycmd = "openssl genrsa -des3 -out mykey.pem 2048"
keyprocess = Popen(keycmd, shell=True, stdin=PIPE, stdout=PIPE)
keyprocess.communicate("password")
keyprocess.communicate("password")
但是它不起作用,脚本只是冻结并且永远不会超过第一个通信语句。
I'm writing a script to generate a CSR in Python. The script is very simple. I generate an RSA private key by using the following:
keycmd = "openssl genrsa -out mykey.pem 2048"
keyprocess = Popen(keycmd, shell=True, stdout=PIPE)
csrcmd = "openssl req -new -key mykey.pem -subj "+ subj + " -out mycsr.csr"
reqprocess = Popen(csrcmd, shell=True, stdout=PIPE)
However, I want to add the functionality to encrypt the private key with a password is the user desires. This is normally done by including the option "-des3" in the genrsa command, but I don't know how to pipe a string from Python standard input to the OpenSSL process. Any help would be appreciated.
What I want to do is:
keycmd = "openssl genrsa -des3 -out mykey.pem 2048"
keyprocess = Popen(keycmd, shell=True, stdin=PIPE, stdout=PIPE)
keyprocess.communicate("password")
keyprocess.communicate("password")
It's not working however, the script just freezes and never gets past the first communicate statement.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将选项 -passout stdin 添加到 openssl genrsa 命令,它将从标准输入读取密码。这应该允许您通过
communicate
发送它。您可以向
-passout
选项提供其他几个值,以从其他来源获取密码。有关详细信息,请参阅 OpenSSL 手册页。Add the option
-passout stdin
to theopenssl genrsa
command, and it will read the passphrase from standard input. That should allow you to send it in viacommunicate
.There are several other values you can provide to the
-passout
option to obtain the passphrase from another source. See the OpenSSL man page for details.您是否尝试过 Pexpect 模块?
Have you tried the Pexpect module ?