vyos.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. if hostname == None:
  77. try:
  78. instance = Instance.objects.get(main=True)
  79. except Instance.DoesNotExist:
  80. return None
  81. hostname = instance.hostname
  82. return hostname
  83. def conntry(hostname):
  84. cmd = {"op": "showConfig", "path": ["interfaces"]}
  85. print(json.dumps(cmd))
  86. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  87. print(post)
  88. print(get_url_retrieve(hostname))
  89. try:
  90. resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
  91. except requests.exceptions.ConnectionError:
  92. return False
  93. print(resp.status_code)
  94. if (resp.status_code == 200):
  95. return True
  96. pprint.pprint(resp)
  97. pprint.pprint(resp.json())
  98. return False
  99. def instance_getall():
  100. instances = Instance.objects.all()
  101. return instances
  102. def get_firewall_all(hostname):
  103. cmd = {"op": "showConfig", "path": ["firewall"]}
  104. firewall_list = api_get(hostname, cmd)
  105. return firewall_list
  106. def set_interface_firewall_ipv4(hostname, interface_type, interface_name, direction, firewall_name):
  107. cmd = {"op": "set", "path": ["interfaces", interface_type, interface_name, "firewall", direction, "name", firewall_name]}
  108. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  109. success = api_set(hostname, cmd)
  110. return success
  111. def delete_interface_firewall_ipv4(hostname, interface_type, interface_name, direction):
  112. cmd = {"op": "delete", "path": ["interfaces", interface_type, interface_name, "firewall", direction]}
  113. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  114. success = api_set(hostname, cmd)
  115. return success
  116. def get_interfaces(hostname):
  117. cmd = {"op": "showConfig", "path": ["interfaces"]}
  118. result1 = api_get(hostname, cmd)
  119. return result1
  120. def get_interface(interface_type, interface_name, hostname):
  121. cmd = {"op": "showConfig", "path": ["interfaces", interface_type, interface_name]}
  122. result1 = api_get(hostname, cmd)
  123. return result1
  124. def get_firewall(hostname, name):
  125. cmd = {"op": "showConfig", "path": ["firewall", "name", name]}
  126. result1 = api_get(hostname, cmd)
  127. return result1
  128. def get_firewall_rule(hostname, name, rulenumber):
  129. cmd = {"op": "showConfig", "path": ["firewall", "name", name, "rule", rulenumber]}
  130. result1 = api_get(hostname, cmd)
  131. return result1
  132. def set_config(hostname, cmd):
  133. #cmd = {"op": "set", "path": ["interface", interface_type, interface_name, "firewall", direction, "name", firewall_name]}
  134. result1 = api_set(hostname, cmd)
  135. return result1
  136. def insert_firewall_rules(hostname, cmd):
  137. pprint.pprint(cmd)
  138. result1 = api_set(hostname, cmd)
  139. return result1
  140. def get_route_static(hostname):
  141. cmd = {"op": "showConfig", "path": ["protocols","static","route"]}
  142. result1 = api_get(hostname, cmd)
  143. return result1
  144. def set_route_static(hostname, subnet, nexthop):
  145. cmd = {"op": "set", "path": ["protocols","static","route", subnet, "next-hop", nexthop]}
  146. result1 = api_set(hostname, cmd)
  147. return result1
  148. def ip_route(hostname):
  149. cmd = {"op": "show", "path": ["ip","route"]}
  150. result1 = api_show(hostname, cmd)
  151. return result1