Skip to content

Instantly share code, notes, and snippets.

@MantasVaitkunas
Last active April 30, 2020 13:46
Show Gist options
  • Save MantasVaitkunas/f92542686f153b9b5091ea63cbdd9248 to your computer and use it in GitHub Desktop.
Save MantasVaitkunas/f92542686f153b9b5091ea63cbdd9248 to your computer and use it in GitHub Desktop.
Script adds ca.crt client.crt and client.key to client.ovpn
# Script generates client.ovpn from given files. Useful when files are generated via easy-rsa.
# Usage: python merge_to_ovpn.py client.ovpn ca.crt client1.crt client1.key
import sys
import os
ovpn_file = sys.argv[1]
ca_file = sys.argv[2]
client_crt = sys.argv[3]
client_key = sys.argv[4]
def read_file(path_to_file):
with open(path_to_file, 'r') as file:
data = file.read()
return data
def is_valid_ovpn_file():
if ovpn_file_contents.find('dev tun') == -1:
return False
return True
def prepare_ovpn_contents():
new_ovpn_file_contents = ovpn_file_contents.replace('ca ca.crt', ca_file_contents)
new_ovpn_file_contents = new_ovpn_file_contents.replace('cert client.crt', crt_file_contents)
new_ovpn_file_contents = new_ovpn_file_contents.replace('key client.key', key_file_contents)
return new_ovpn_file_contents
ovpn_file_contents = read_file(ovpn_file)
ca_file_contents = '<ca>\n'+read_file(ca_file)+'\n</ca>'
crt_file_contents = '<cert>\n'+read_file(client_crt)+'\n</cert>'
key_file_contents = '<key>\n'+read_file(client_key)+'\n</key>'
if not is_valid_ovpn_file():
sys.exit("ERROR: ovpn file is not valid, or not provided.")
new_ovpn_file_contents = prepare_ovpn_contents()
ovpn_file_name = os.path.basename(client_crt)
splitted_name = ovpn_file_name.split('.')
del splitted_name[-1]
newly_generated_file_name = ".".join(splitted_name)+'.ovpn'
with open(newly_generated_file_name, "w+") as f:
f.write(new_ovpn_file_contents)
print 'File '+newly_generated_file_name+' is generated successfully!'
~
~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment