1
#!/usr/bin/python

import os
import sys

client=sys.argv[1]
homeDir="/home/vpnUsers"

os.system("cd /etc/openvpn/easy-rsa/")
os.system("./easyrsa build-client-full %s" %(client))
os.system("cp /etc/openvpn/client-template.txt %s/%s.ovpn" %(homeDir,client))

At os.system("./easyrsa build-client-full %s" %(client)) if I run it manually on terminal it ask 2 times for a password, so If I do it manually I can enter a password, click enter and its done. How can I do this but with python?

Lets supose the sys.argv[2] its the password and it should be written at the this command when it ask for password.

Ps: the args come from php.

4
  • have you try with stream? some_command < file_with_parameters Commented Feb 17, 2020 at 11:28
  • At php I only do $output = exec("/path/to/file.sh $client 2>&1"); (with escapeshellarg obviously). I don't really understand what you asking. How can I send more parameters to that exact os.system? Commented Feb 17, 2020 at 11:44
  • I mean If os.system("./easyrsa build-client-full %s" %(client)) is asking for user input, is it possible to send args to that questions? Commented Feb 17, 2020 at 11:45
  • If your command asking for 2 args put them to the file (separate them with white sign) and run it command < file_with_args. It should works! Commented Feb 17, 2020 at 20:17

1 Answer 1

1

Solved it!

Pexpect

This library will talk with the script. Easy to use.

pip install pexpect

Then on your script python just import it

import pexpect

Example code for my situation

 localcmd='/path/to/script/easyrsa build-client-full "arg"'

 def localOutput(localcmd):
        child = pexpect.spawn (localcmd)
        child.expect ('Enter PEM pass phrase:')
        child.sendline ('password')
        child.expect ('Verifying - Enter PEM pass phrase:')
        child.sendline ('password')
        child.expect(pexpect.EOF)
        return child.before  

localout=localOutput(localcmd)
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.