import os import json import argparse def get_all_files(path, extension=".ovpn"): result = [os.path.join(dp, f) for dp, dn, filenames in os.walk( path) for f in filenames if os.path.splitext(f)[-1] == extension] return result def gen_cfg(cfg_fp, auth_file): filename = os.path.split(cfg_fp)[-1] name = os.path.splitext(filename)[0] return { "cfg_fp": cfg_fp, "name": name, "additional_cfg": { "auth-user-pass": auth_file } } # Construct the argument parser ap = argparse.ArgumentParser() ap.add_argument("-c", "--config_folder", required=True, help="path to the ovpn config files") ap.add_argument("-a", "--auth_file", required=True, help="path to the auth-user-pass file") args = vars(ap.parse_args()) config_folder = args["config_folder"] auth_file = args["auth_file"] all_fps = get_all_files(config_folder) cfgs = [] for cfg_fp in all_fps: cfgs.append(gen_cfg(cfg_fp, auth_file)) cfgs.sort(key=lambda x: x["name"]) profile_fp = "profiles.json" open(profile_fp, "w").write(json.dumps(cfgs))