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.7KB

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