使用pexpect自动化openssl req -new -key server.key -out server.csr

时间:2012-08-31 12:43:14

标签: python openssl

我正在尝试自动化下面的拨号。我确实关心第一个密码输入的内容。我很难找到合适的工作。

root@ffffff:/home/ubuntu# openssl req -new -key server.key -out server.csr
Enter pass phrase for server.key:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:
State or Province Name (full name) [Some-State]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

以下是我的pexpect代码:

  import pexpect
child = pexpect.spawn ('openssl req -new -key server.key -out server.csr')
child.expect ('key*')
child.sendline ('YYYY')
child.expect ("Country Name (2 letter code) [AU]:")
child.sendline ('US')
child.expect ("State or Province Name (full name) [Some-State]:")
child.sendline ('HI')
child.expect ("Locality Name (eg, city) []:")
child.sendline ('US')
child.expect ("Organization Name (eg, company) [Internet Widgits Pty Ltd]:")
child.sendline ('US')
child.expect ("Organizational Unit Name (eg, section) []:")
child.sendline ('US')
child.expect ("Common Name (e.g. server FQDN or YOUR name) []:")
child.sendline ('US')
child.expect ("Email Address []:")
child.sendline ('US')
child.expect ("A challenge password []:")
child.sendline ("yy")
child.expect ("An optional company name []:")
child.sendline ("yy")
child.expect(pexpect.EOF)

以下是我的错误:

challenge password []:
An optional company name []:
root@li494-175:/home/ubuntu# clear
root@li494-175:/home/ubuntu# python vv.py
Traceback (most recent call last):
  File "vv.py", line 5, in <module>
    child.expect ("Country Name (2 letter code) [AU]:")
  File "/usr/local/lib/python2.7/dist-packages/pexpect-2.4-py2.7.egg/pexpect.py", line 1316, in expect
    return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
  File "/usr/local/lib/python2.7/dist-packages/pexpect-2.4-py2.7.egg/pexpect.py", line 1330, in expect_list
    return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize)
  File "/usr/local/lib/python2.7/dist-packages/pexpect-2.4-py2.7.egg/pexpect.py", line 1401, in expect_loop
    raise EOF (str(e) + '\n' + str(self))
pexpect.EOF: End Of File (EOF) in read_nonblocking(). Exception style platform.
<pexpect.spawn object at 0x2083850>
version: 2.4 ($Revision: 516 $)
command: /usr/bin/openssl
args: ['/usr/bin/openssl', 'req', '-new', '-key', 'server.key', '-out', 'server.csr']
searcher: searcher_re:
    0: re.compile("Country Name (2 letter code) [AU]:")
buffer (last 100 chars):
before (last 100 chars): p_enc.c:539:
140006640854688:error:0906A065:PEM routines:PEM_do_header:bad decrypt:pem_lib.c:476:

after: <class 'pexpect.EOF'>
match: None

1 个答案:

答案 0 :(得分:0)

为什么不用配置文件尝试上述内容?

openssl req -new -key server.key -out server.csr -config config.txt

http://webservices.itcs.umich.edu/mediawiki/radmind/index.php/Example_OpenSSL_Configuration_File