You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vpngen-cli.py 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #! /usr/bin/env python3
  2. from __future__ import print_function
  3. import argparse
  4. import json
  5. import sys
  6. import vpngen
  7. def eprint(*args, **kwargs):
  8. print(*args, file=sys.stderr, **kwargs)
  9. def create_variables(variables, defaults):
  10. variables_set = defaults.copy()
  11. for variable in variables:
  12. if variable == 'name':
  13. continue
  14. default = variables_set[variable] if variable in variables_set else ''
  15. print("Enter a value for '%s' [%s]: " % (variable, default), end='', flush=True)
  16. value = sys.stdin.readline()[:-1]
  17. if value != '':
  18. variables_set[variable] = value
  19. return variables_set
  20. def main():
  21. parser = argparse.ArgumentParser(description='Manage OpenVPN VPNs')
  22. parser.add_argument('--vpn', help='The VPN to use', required=True)
  23. parser.add_argument('--config', dest='config', default='/etc/vpngen/vpngen.json', help='Configuration file path')
  24. parser.add_argument('--create', help='Create a VPN', action='store_true')
  25. parser.add_argument('--remove', help='Remove a VPN', action='store_true')
  26. parser.add_argument('--create-client', help='Create a client for the VPN', metavar='CLIENT')
  27. parser.add_argument('--remove-client', help='Remove a client for the VPN', metavar='CLIENT')
  28. parser.add_argument('--rebuild-clients', help='Rebuild clients configurations', action='store_true')
  29. args = parser.parse_args()
  30. with open(args.config, "r") as f:
  31. config = json.load(f)
  32. vpn_name = config['vpnPrefix'] + args.vpn + config['vpnSuffix']
  33. if args.create_client is not None:
  34. client_name = args.create_client
  35. elif args.remove_client is not None:
  36. client_name = args.remove_client
  37. else:
  38. client_name = None
  39. if client_name is not None:
  40. client_name = config['clientPrefix'] + client_name + config['clientSuffix']
  41. vpng = vpngen.VpnGen(config['defaultConfigPath'], config['ovpnConfigPath'])
  42. if args.create:
  43. variables = create_variables(vpng.get_vpn_vars(), config['defaults']['vpn'])
  44. res = vpng.create_vpn(vpn_name, variables)
  45. if res == vpngen.VpnGenError.Success:
  46. print("VPN %s created successfully" % vpn_name)
  47. else:
  48. eprint("Failed to create VPN %s: %s" % (vpn_name, res))
  49. exit(1)
  50. elif args.remove:
  51. res = vpng.remove_vpn(vpn_name)
  52. if res == vpngen.VpnGenError.Success:
  53. print("VPN %s removed successfully" % vpn_name)
  54. else:
  55. eprint("Failed to remove VPN %s: %s" % (vpn_name, res))
  56. exit(1)
  57. elif args.create_client:
  58. variables = create_variables(vpng.get_vpn_vars(), config['defaults']['vpn'])
  59. res = vpng.create_client(vpn_name, client_name, variables)
  60. if res == vpngen.VpnGenError.Success:
  61. print("Client %s created successfully on VPN %s" % (client_name, vpn_name))
  62. else:
  63. eprint("Failed to create client %s on VPN %s: %s" % (client_name, vpn_name, res))
  64. exit(1)
  65. elif args.remove_client:
  66. res = vpng.remove_client(vpn_name, client_name)
  67. if res == vpngen.VpnGenError.Success:
  68. print("Client %s removed successfully on VPN %s" % (client_name, vpn_name))
  69. else:
  70. eprint("Failed to remove client %s on VPN %s: %s" % (client_name, vpn_name, res))
  71. exit(1)
  72. elif args.rebuild_clients:
  73. res = vpng.rebuild_clients(vpn_name)
  74. if res == vpngen.VpnGenError.Success:
  75. print("Clients configurations rebuilt successfully on VPN %s" % vpn_name)
  76. else:
  77. eprint("Failed to rebuild clients configuration on VPN %s: %s" % (vpn_name, res))
  78. exit(1)
  79. main()