首页 > 解决方案 > 从需要用户输入的python运行linux命令

问题描述

我有以下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执行命令行,发送输入和读取输出

如果您在 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:

标签: pythonlinuxpython-3.xpython-2.7openssl

解决方案



推荐阅读