Skip to content

Instantly share code, notes, and snippets.

@ofirule
Last active December 25, 2023 19:46
Show Gist options
  • Save ofirule/8cb5765ca964cf751a0b2e2dc50a341e to your computer and use it in GitHub Desktop.
Save ofirule/8cb5765ca964cf751a0b2e2dc50a341e to your computer and use it in GitHub Desktop.
converting openvpn .conf file to .ovpn file
import sys, os
ALLOW_FILE_OPTIONS = ["ca", "cert", "dh", "extra-certs", "key", "pkcs12", "secret", "crl-verify", "http-proxy-user-pass", "tls-auth", "tls-crypt"]
filepath = sys.argv[1]
output_file = os.path.basename(filepath). replace(".conf", ".ovpn")
inline_tuples_to_add = []
with open(output_file, 'w') as dst:
with open(filepath) as src:
for l in src:
option = l.split()
if len(option) >= 2 and option[0] in ALLOW_FILE_OPTIONS and os.path.isfile(option[1]):
inline_tuples_to_add.append((option[0], option[1]))
continue
dst.write(l)
dst.write("key-direction 1\n\n") # needed fot tls-auth
for t in inline_tuples_to_add :
tag_begining = "<{}>\n".format(t[0])
dst.write(tag_begining)
with open(t[1]) as tag_cpntent_file:
dst.writelines(tag_cpntent_file.readlines())
tag_ending = "</{}>\n\n".format(t[0])
dst.write(tag_ending)
@ofirule
Copy link
Author

ofirule commented Sep 26, 2019

Running example:
python convert_openvpn_conf_file_to_ovpn_file.py /etc/openvpn/client.conf

would create a .ovpn file on current working directory

@alexandrewillame
Copy link

alexandrewillame commented Dec 25, 2023

This line is problematic if the extension is not .conf:
output_file = os.path.basename(filepath). replace(".conf", ".ovpn")

I would recommend doing the following:

output_file = os.path.splitext(filepath)[0] + ".ovpn"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment