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

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