【发布时间】:2018-10-25 12:36:26
【问题描述】:
我有以下 python 代码:
import subprocess
p=subprocess.Popen(["openssl","genrsa","-aes256","-out","ise.key.pem","2048"],stdin=subprocess.PIPE,stdout=subprocess.PIPE)
inputdata="123456"
p.communicate(input=inputata)
p.communicate(input=inputata)
以上代码的输出是:
pan@pan:~/python-scripts$ Generating RSA private key, 2048 bit long modulus
........................+++
...................................+++
e is 65537 (0x10001)
Enter pass phrase for ise.key.pem:
User interface error
140617148802712:error:0906906F:PEM routines:PEM_ASN1_write_bio:read key:pem_lib.c:379:
我希望输入应该由 python 提供,不应该有任何用户交互:
我知道证书可以由 python pyOpenSSL 库生成,但我想使用普通的 linux 命令
我的python版本是:
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "enter code here`license" for more information.
我看到了以下链接,但没有帮助
Python execute command line,sending input and reading output
如果你在 shell 中运行命令,它会运行如下:
pan@pan:~/python-scripts$ openssl genrsa -aes256 -out ise.key.pem 2048
Generating RSA private key, 2048 bit long modulus
..............................................................+++
..+++
e is 65537 (0x10001)
Enter pass phrase for ise.key.pem:
Verifying - Enter pass phrase for ise.key.pem:
【问题讨论】:
-
shell=True 允许解决错误输入您的进程:参数列表应为
["openssl","genrsa","-aes256","-out","ise.key.pem","2048"]。但真正的问题是管道输入到密码输入例程并不总是有效。您必须在较低级别生成键盘事件。 -
我建议您尝试上述方法,然后edit 您的问题删除关于 shell=True 的无用部分,这会使您的问题成倍增加。
标签: python linux python-3.x python-2.7 openssl