vyos.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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_show(hostname):
  24. url = get_url(hostname) + '/show'
  25. return url
  26. def get_url_retrieve(hostname):
  27. url = get_url(hostname) + '/retrieve'
  28. return url
  29. def get_key(hostname):
  30. # permcheck
  31. instance = Instance.objects.get(hostname=hostname)
  32. return instance.key
  33. def api(type, hostname, cmd):
  34. if type == "retrieve":
  35. url = get_url_retrieve(hostname)
  36. elif type == "manage":
  37. url = get_url_manage(hostname)
  38. elif type == "configure":
  39. url = get_url_configure(hostname)
  40. elif type == "show":
  41. url = get_url_show(hostname)
  42. else:
  43. return False
  44. pprint.pprint(cmd)
  45. print(json.dumps(cmd))
  46. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  47. print(post)
  48. try:
  49. resp = requests.post(url, verify=False, data=post, timeout=5)
  50. except requests.exceptions.ConnectionError:
  51. return False
  52. print(resp.status_code)
  53. pprint.pprint(resp)
  54. pprint.pprint(resp.json())
  55. if resp.status_code != 200:
  56. # This means something went wrong.
  57. #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
  58. return False
  59. #for todo_item in resp.json():
  60. #print('{} {}'.format(todo_item['id'], todo_item['summary']))
  61. result1 = resp.json()
  62. print(result1['data'])
  63. #result2 = json.loads(result1['data'])
  64. pprint.pprint(result1)
  65. return result1['data']
  66. def api_get(hostname, cmd):
  67. return api('retrieve', hostname, cmd)
  68. def api_show(hostname, cmd):
  69. return api('show', hostname, cmd)
  70. def api_set(hostname, cmd):
  71. return api('configure', hostname, cmd)
  72. def get_hostname_prefered(request):
  73. hostname = None
  74. #if request.session.get('hostname', None) != None:
  75. # hostname = request.session.get('hostname', None)
  76. hostname = '179.127.12.142'
  77. if hostname == None:
  78. try:
  79. instance = Instance.objects.get(main=True)
  80. except Instance.DoesNotExist:
  81. return None
  82. hostname = instance.hostname
  83. return hostname
  84. def conntry(hostname):
  85. cmd = {"op": "showConfig", "path": ["interfaces"]}
  86. print(json.dumps(cmd))
  87. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  88. print(post)
  89. print(get_url_retrieve(hostname))
  90. try:
  91. resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=10)
  92. except requests.exceptions.ConnectionError:
  93. return False
  94. print(resp.status_code)
  95. if (resp.status_code == 200):
  96. return True
  97. pprint.pprint(resp)
  98. pprint.pprint(resp.json())
  99. return False
  100. def instance_getall():
  101. instances = Instance.objects.all()
  102. return instances
  103. def get_firewall_all(hostname):
  104. cmd = {"op": "showConfig", "path": ["firewall"]}
  105. firewall_list = api_get(hostname, cmd)
  106. return firewall_list
  107. def set_interface_firewall_ipv4(hostname, interface_type, interface_name, direction, firewall_name):
  108. cmd = {"op": "set", "path": ["interfaces", interface_type, interface_name, "firewall", direction, "name", firewall_name]}
  109. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  110. success = api_set(hostname, cmd)
  111. return success
  112. def delete_interface_firewall_ipv4(hostname, interface_type, interface_name, direction):
  113. cmd = {"op": "delete", "path": ["interfaces", interface_type, interface_name, "firewall", direction]}
  114. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  115. success = api_set(hostname, cmd)
  116. return success
  117. def get_interfaces(hostname):
  118. cmd = {"op": "showConfig", "path": ["interfaces"]}
  119. result1 = api_get(hostname, cmd)
  120. return result1
  121. def get_interface(interface_type, interface_name, hostname):
  122. cmd = {"op": "showConfig", "path": ["interfaces", interface_type, interface_name]}
  123. result1 = api_get(hostname, cmd)
  124. return result1
  125. def get_firewall(hostname, name):
  126. cmd = {"op": "showConfig", "path": ["firewall", "name", name]}
  127. result1 = api_get(hostname, cmd)
  128. return result1
  129. def get_firewall_rule(hostname, name, rulenumber):
  130. cmd = {"op": "showConfig", "path": ["firewall", "name", name, "rule", rulenumber]}
  131. result1 = api_get(hostname, cmd)
  132. return result1
  133. def set_config(hostname, cmd):
  134. #cmd = {"op": "set", "path": ["interface", interface_type, interface_name, "firewall", direction, "name", firewall_name]}
  135. result1 = api_set(hostname, cmd)
  136. return result1
  137. def insert_firewall_rules(hostname, cmd):
  138. pprint.pprint(cmd)
  139. result1 = api_set(hostname, cmd)
  140. return result1
  141. def get_route_static(hostname):
  142. cmd = {"op": "showConfig", "path": ["protocols","static","route"]}
  143. result1 = api_get(hostname, cmd)
  144. return result1
  145. def set_route_static(hostname, subnet, nexthop):
  146. cmd = {"op": "set", "path": ["protocols","static","route", subnet, "next-hop", nexthop]}
  147. result1 = api_set(hostname, cmd)
  148. return result1
  149. def delete_route_static(hostname, subnet, nexthop):
  150. #cmd = {"op": "delete", "path": ["protocols","static","route", subnet, "next-hop", nexthop]}
  151. cmd = {"op": "delete", "path": ["protocols","static","route", subnet]}
  152. result1 = api_set(hostname, cmd)
  153. return result1
  154. def delete_route_rule(hostname, firewall_name, rule_name):
  155. cmd = {"op": "delete", "path": ["firewall", "name", firewall_name, "rule", rule_name]}
  156. result1 = api_set(hostname, cmd)
  157. return result1
  158. def delete_firewall(hostname, name):
  159. cmd = {"op": "delete", "path": ["firewall","name", name]}
  160. result1 = api_set(hostname, cmd)
  161. return result1
  162. def ip_route(hostname):
  163. cmd = {"op": "show", "path": ["ip","route"]}
  164. result1 = api_show(hostname, cmd)
  165. return result1