vyos.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import requests
  2. import json
  3. import pprint
  4. import sys
  5. from config.models import Instance
  6. def get_url(hostname):
  7. # permcheck
  8. instance = Instance.objects.get(hostname=hostname)
  9. if instance.https == True:
  10. protocol = "https"
  11. else:
  12. protocol = "http"
  13. if (instance.port == None):
  14. instance.port = 443
  15. url = protocol + "://" + instance.hostname + ":" + str(instance.port)
  16. return url
  17. def get_url_manage(hostname):
  18. url = get_url(hostname) + '/config-file'
  19. return url
  20. def get_url_configure(hostname):
  21. url = get_url(hostname) + '/configure'
  22. return url
  23. def get_url_retrieve(hostname):
  24. url = get_url(hostname) + '/retrieve'
  25. return url
  26. def get_key(hostname):
  27. # permcheck
  28. instance = Instance.objects.get(hostname=hostname)
  29. return instance.key
  30. def api(type, hostname, cmd):
  31. if type == "retrieve":
  32. url = get_url_retrieve(hostname)
  33. elif type == "manage":
  34. url = get_url_manage(hostname)
  35. elif type == "configure":
  36. url = get_url_configure(hostname)
  37. else:
  38. return False
  39. pprint.pprint(cmd)
  40. print(json.dumps(cmd))
  41. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  42. print(post)
  43. try:
  44. resp = requests.post(url, verify=False, data=post, timeout=5)
  45. except requests.exceptions.ConnectionError:
  46. return False
  47. print(resp.status_code)
  48. pprint.pprint(resp)
  49. pprint.pprint(resp.json())
  50. if resp.status_code != 200:
  51. # This means something went wrong.
  52. #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
  53. return False
  54. #for todo_item in resp.json():
  55. #print('{} {}'.format(todo_item['id'], todo_item['summary']))
  56. result1 = resp.json()
  57. print(result1['data'])
  58. #result2 = json.loads(result1['data'])
  59. pprint.pprint(result1)
  60. return result1['data']
  61. def api_get(hostname, cmd):
  62. return api('retrieve', hostname, cmd)
  63. def api_set(hostname, cmd):
  64. return api('configure', hostname, cmd)
  65. def get_hostname_prefered(request):
  66. hostname = None
  67. if request.session.get('hostname', None) != None:
  68. hostname = request.session.get('hostname', None)
  69. if hostname == None:
  70. try:
  71. instance = Instance.objects.get(main=True)
  72. except Instance.DoesNotExist:
  73. return None
  74. hostname = instance.hostname
  75. return hostname
  76. def conntry(hostname):
  77. cmd = {"op": "showConfig", "path": ["interfaces"]}
  78. print(json.dumps(cmd))
  79. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  80. print(post)
  81. print(get_url_retrieve(hostname))
  82. try:
  83. resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
  84. except requests.exceptions.ConnectionError:
  85. return False
  86. print(resp.status_code)
  87. if (resp.status_code == 200):
  88. return True
  89. pprint.pprint(resp)
  90. pprint.pprint(resp.json())
  91. return False
  92. def instance_getall():
  93. instances = Instance.objects.all()
  94. return instances
  95. def get_firewall_all(hostname):
  96. cmd = {"op": "showConfig", "path": ["firewall"]}
  97. firewall_list = api_get(hostname, cmd)
  98. return firewall_list
  99. def set_interface_firewall_ipv4(hostname, interface_type, interface_name, direction, firewall_name):
  100. cmd = {"op": "set", "path": ["interfaces", interface_type, interface_name, "firewall", direction, "name", firewall_name]}
  101. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  102. success = api_set(hostname, cmd)
  103. return success
  104. def delete_interface_firewall_ipv4(hostname, interface_type, interface_name, direction):
  105. cmd = {"op": "delete", "path": ["interfaces", interface_type, interface_name, "firewall", direction]}
  106. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  107. success = api_set(hostname, cmd)
  108. return success
  109. def get_interfaces(hostname):
  110. cmd = {"op": "showConfig", "path": ["interfaces"]}
  111. result1 = api_get(hostname, cmd)
  112. return result1
  113. def get_interface(interface_type, interface_name, hostname):
  114. cmd = {"op": "showConfig", "path": ["interfaces", interface_type, interface_name]}
  115. result1 = api_get(hostname, cmd)
  116. return result1
  117. def get_firewall(hostname, name):
  118. cmd = {"op": "showConfig", "path": ["firewall", "name", name]}
  119. result1 = api_get(hostname, cmd)
  120. return result1
  121. def get_firewall_rule(hostname, name, rulenumber):
  122. cmd = {"op": "showConfig", "path": ["firewall", "name", name, "rule", rulenumber]}
  123. result1 = api_get(hostname, cmd)
  124. return result1
  125. def set_config(hostname, cmd):
  126. #cmd = {"op": "set", "path": ["interface", interface_type, interface_name, "firewall", direction, "name", firewall_name]}
  127. result1 = api_set(hostname, cmd)
  128. return result1
  129. def insert_firewall_rules(hostname, cmd):
  130. pprint.pprint(cmd)
  131. result1 = api_set(hostname, cmd)
  132. return result1
  133. def get_route_static(hostname):
  134. cmd = {"op": "showConfig", "path": ["show","protocols","route","static"]}
  135. result1 = api_get(hostname, cmd)
  136. return result1
  137. def set_route_static(hostname, subnet, nexthop):
  138. cmd = {"op": "set", "path": ["set","protocols","static","route", subnet, "next-hop", nexthop]}
  139. result1 = api_set(hostname, cmd)
  140. return result1