Skip to content

Instantly share code, notes, and snippets.

@RealOrangeOne
Last active July 19, 2016 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RealOrangeOne/050da86871fb952ba7bfe97eece8555c to your computer and use it in GitHub Desktop.
Save RealOrangeOne/050da86871fb952ba7bfe97eece8555c to your computer and use it in GitHub Desktop.
Extract certtificates and keys from astrill OVPN files. Should work for others
import os, shutil
from glob import glob
def write_file(data, out_file_name, new_dir):
with open(os.path.join(new_dir, out_file_name), 'w') as f:
f.write(''.join(data))
for ovpn_file in glob('*.ovpn'):
new_dir = os.path.join(ovpn_file.replace('.ovpn', ''))
os.makedirs(new_dir, exist_ok=True)
shutil.copy2(ovpn_file, os.path.join(new_dir, ovpn_file.split('/')[-1]))
with open(ovpn_file) as f:
file_data = f.readlines()
in_block = False
data = []
for line in file_data:
if line.startswith('<') and line.strip().endswith('>'): # if this is a tag line
if in_block: # we're in a block, write data
file_ext = line.strip().replace('</', '').replace('>', '') + ".pem"
write_file(data, ovpn_file.split('/')[-1] + '-' + file_ext, new_dir)
data = []
in_block = False
else:
in_block = True
elif in_block:
data.append(line)
os.remove(ovpn_file)
print("Exported {}".format(ovpn_file.split('/')[-1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment