0

While generating key/cert you should press few times 'enter' and in the very end press "yes". How to do it in code like this?

buildkey = ["printf '\n\n\n\n\n\n\n\n\n\n ' | /etc/openvpn/easy-rsa/build-key"]
runBuildKey = subprocess.Popen(buildkey, shell=True )
13
  • I'm not sure what the square brackets are doing there, but apart from that, the approach looks like it has legs; what happens when you try your version? Commented Jan 11, 2016 at 22:18
  • 1
    This is the wrong place to start out-of-the-gate. You should be editing your openssl.cnf (since you're using easy-rsa, you'll have one) to get all its input from environment variables, so you don't need to "press enter" on stdin at all. Commented Jan 11, 2016 at 22:18
  • @ Charles Duffy , what do you mean :"You should be editing your openssl.cnf" Commented Jan 11, 2016 at 22:21
  • In /etc/openvpn/easy-rsa, see the file openssl.cnf? Commented Jan 11, 2016 at 22:21
  • I still don`t see where I could switch "y/n" prompt off in openssl.cnf .. Commented Jan 11, 2016 at 22:28

1 Answer 1

3

Don't try to edit stdin at all here. Instead, open up your openssl.cnf, and modify it to get all the input you need from the environment, like so:

[ req_distinguished_name ]
countryName_default             = $ENV::SSL_countryName
stateOrProvinceName_default     = $ENV::SSL_stateOrProvinenceName

...and so forth. Once this is done, set the variables in your environment before calling build-key with the argument -batch. In bash, that might look like so:

SSL_countryName=foo SSL_stateOrProvinenceName=bar build-key -batch </dev/null

Alternately, in Python, you can do the same thing via arguments to subprocess.Popen:

subprocess.call(['/etc/openvpn/easy-rsa/build-key', '-batch'], env={
    'SSL_countryName': 'foo',
    'SSL_stateOrProvinenceName': 'bar',
    # ...and so forth for any other $ENV::* setting you want to override
}, stdin=open('/dev/null', 'r'))

However, if you really want to pass a custom stream through for stdin, you can do that -- without any shell pipeline at all:

p = subprocess.Popen(['/etc/openvpn/easy-rsa/build-key'], stdin=subprocess.PIPE)
p.communicate('\n'.join(['', '', '', '', '', 'yes', '']))
#                        ^^
#   use one of these for each item you want to press enter to before the "yes"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.