vyos.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. import requests
  2. import json
  3. import pprint
  4. import sys
  5. from config.models import Instance
  6. from django.contrib.auth.models import Group
  7. from django.contrib.auth.models import User
  8. import perms
  9. def instance_getall(*args, **kwargs):
  10. return perms.instance_getall(*args, **kwargs)
  11. def get_hostname_prefered(*args, **kwargs):
  12. return perms.get_hostname_prefered(*args, **kwargs)
  13. def instance_getall_by_group(*args, **kwargs):
  14. return perms.instance_getall_by_group(*args, **kwargs)
  15. def repvar(s):
  16. return s.replace("-", "_")
  17. def get_url(hostname):
  18. # permcheck
  19. instance = Instance.objects.get(hostname=hostname)
  20. if instance.https == True:
  21. protocol = "https"
  22. else:
  23. protocol = "http"
  24. if (instance.port == None):
  25. instance.port = 443
  26. url = protocol + "://" + instance.hostname + ":" + str(instance.port)
  27. return url
  28. def get_url_manage(hostname):
  29. url = get_url(hostname) + '/config-file'
  30. return url
  31. def get_url_configure(hostname):
  32. url = get_url(hostname) + '/configure'
  33. return url
  34. def get_url_show(hostname):
  35. url = get_url(hostname) + '/show'
  36. return url
  37. def get_url_retrieve(hostname):
  38. url = get_url(hostname) + '/retrieve'
  39. return url
  40. def get_key(hostname):
  41. # permcheck
  42. instance = Instance.objects.get(hostname=hostname)
  43. return instance.key
  44. def api(type, hostname, cmd):
  45. if type == "retrieve":
  46. url = get_url_retrieve(hostname)
  47. elif type == "manage":
  48. url = get_url_manage(hostname)
  49. elif type == "configure":
  50. url = get_url_configure(hostname)
  51. elif type == "show":
  52. url = get_url_show(hostname)
  53. else:
  54. return False
  55. pprint.pprint(cmd)
  56. print(json.dumps(cmd))
  57. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  58. print(post)
  59. try:
  60. resp = requests.post(url, verify=False, data=post, timeout=10)
  61. except requests.exceptions.ConnectionError:
  62. return False
  63. print(resp.status_code)
  64. pprint.pprint(resp)
  65. pprint.pprint(resp.json())
  66. if resp.status_code != 200:
  67. # This means something went wrong.
  68. #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
  69. return False
  70. #for todo_item in resp.json():
  71. #print('{} {}'.format(todo_item['id'], todo_item['summary']))
  72. result1 = resp.json()
  73. print(result1['data'])
  74. #result2 = json.loads(result1['data'])
  75. pprint.pprint(result1)
  76. return result1['data']
  77. def api_get(hostname, cmd):
  78. return api('retrieve', hostname, cmd)
  79. def api_show(hostname, cmd):
  80. return api('show', hostname, cmd)
  81. def api_set(hostname, cmd):
  82. return api('configure', hostname, cmd)
  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=10)
  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 get_firewall_all(hostname):
  100. cmd = {"op": "showConfig", "path": ["firewall"]}
  101. firewall_list = api_get(hostname, cmd)
  102. nfirewall_list = {}
  103. for f in firewall_list:
  104. s = repvar(f)
  105. nfirewall_list[s] = firewall_list[f]
  106. nfirewall_list[f] = firewall_list[f]
  107. return nfirewall_list
  108. def set_interface_firewall_ipv4(hostname, interface_type, interface_name, direction, firewall_name):
  109. cmd = {"op": "set", "path": ["interfaces", interface_type, interface_name, "firewall", direction, "name", firewall_name]}
  110. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  111. success = api_set(hostname, cmd)
  112. return success
  113. def delete_interface_firewall_ipv4(hostname, interface_type, interface_name, direction):
  114. cmd = {"op": "delete", "path": ["interfaces", interface_type, interface_name, "firewall", direction]}
  115. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  116. success = api_set(hostname, cmd)
  117. return success
  118. def get_interfaces(hostname):
  119. cmd = {"op": "showConfig", "path": ["interfaces"]}
  120. result1 = api_get(hostname, cmd)
  121. return result1
  122. def get_interface(interface_type, interface_name, hostname):
  123. cmd = {"op": "showConfig", "path": ["interfaces", interface_type, interface_name]}
  124. result1 = api_get(hostname, cmd)
  125. return result1
  126. def get_firewall(hostname, name):
  127. cmd = {"op": "showConfig", "path": ["firewall", "name", name]}
  128. result1 = api_get(hostname, cmd)
  129. return result1
  130. def get_firewall_rule(hostname, name, rulenumber):
  131. cmd = {"op": "showConfig", "path": ["firewall", "name", name, "rule", rulenumber]}
  132. result1 = api_get(hostname, cmd)
  133. return result1
  134. def set_config(hostname, cmd):
  135. #cmd = {"op": "set", "path": ["interface", interface_type, interface_name, "firewall", direction, "name", firewall_name]}
  136. result1 = api_set(hostname, cmd)
  137. return result1
  138. def insert_firewall_rules(hostname, cmd):
  139. pprint.pprint(cmd)
  140. result1 = api_set(hostname, cmd)
  141. return result1
  142. def get_route_static(hostname):
  143. cmd = {"op": "showConfig", "path": ["protocols","static","route"]}
  144. result1 = api_get(hostname, cmd)
  145. return result1
  146. def set_firewall_syncookies_enable(hostname):
  147. cmd = {"op": "set", "path": ["firewall","syn-cookies",'enable']}
  148. result1 = api_set(hostname, cmd)
  149. return result1
  150. def set_firewall_syncookies_disable(hostname):
  151. cmd = {"op": "set", "path": ["firewall","syn-cookies",'disable']}
  152. result1 = api_set(hostname, cmd)
  153. return result1
  154. def set_firewall_allping_enable(hostname):
  155. cmd = {"op": "set", "path": ["firewall","all-ping",'enable']}
  156. result1 = api_set(hostname, cmd)
  157. return result1
  158. def set_firewall_allping_disable(hostname):
  159. cmd = {"op": "set", "path": ["firewall","all-ping",'disable']}
  160. result1 = api_set(hostname, cmd)
  161. return result1
  162. def get_firewall_portgroup(hostname):
  163. cmd = {"op": "showConfig", "path": ["firewall","group","port-group"]}
  164. result1 = api_get(hostname, cmd)
  165. return result1
  166. def set_firewall_portgroup_del(hostname, group_name):
  167. cmd = {"op": "delete", "path": ["firewall","group",'port-group', group_name]}
  168. result1 = api_set(hostname, cmd)
  169. return result1
  170. def set_firewall_portgroup_description(hostname, group_name, description):
  171. cmd = {"op": "set", "path": ["firewall","group",'port-group', group_name, "description", description]}
  172. result1 = api_set(hostname, cmd)
  173. return result1
  174. def set_firewall_portgroup_add(hostname, group_name, port):
  175. cmd = {"op": "set", "path": ["firewall","group",'port-group', group_name, "port", port]}
  176. result1 = api_set(hostname, cmd)
  177. return result1
  178. def set_firewall_portgroup_delete_port(hostname, group_name, port):
  179. cmd = {"op": "delete", "path": ["firewall","group",'port-group', group_name, "port", port]}
  180. result1 = api_set(hostname, cmd)
  181. return result1
  182. def get_firewall_addressgroup(hostname):
  183. cmd = {"op": "showConfig", "path": ["firewall","group","address-group"]}
  184. result1 = api_get(hostname, cmd)
  185. return result1
  186. def get_firewall_networkgroup(hostname):
  187. cmd = {"op": "showConfig", "path": ["firewall","group","network-group"]}
  188. result1 = api_get(hostname, cmd)
  189. return result1
  190. def get_firewall_addressgroup_one(hostname, group_name):
  191. cmd = {"op": "showConfig", "path": ["firewall","group","address-group", group_name]}
  192. result1 = api_get(hostname, cmd)
  193. return result1
  194. def get_firewall_networkgroup_one(hostname, group_name):
  195. cmd = {"op": "showConfig", "path": ["firewall","group","network-group", group_name]}
  196. result1 = api_get(hostname, cmd)
  197. return result1
  198. def set_firewall_networkgroup_description(hostname, group_name, description):
  199. cmd = {"op": "set", "path": ["firewall","group",'network-group', group_name, "description", description]}
  200. result1 = api_set(hostname, cmd)
  201. return result1
  202. def set_firewall_addressgroup_description(hostname, group_name, description):
  203. cmd = {"op": "set", "path": ["firewall","group",'address-group', group_name, "description", description]}
  204. result1 = api_set(hostname, cmd)
  205. return result1
  206. def set_firewall_addressgroup_add(hostname, group_name, address):
  207. cmd = {"op": "set", "path": ["firewall","group",'address-group', group_name, "address", address]}
  208. result1 = api_set(hostname, cmd)
  209. return result1
  210. def set_firewall_addressgroup_del(hostname, group_name):
  211. cmd = {"op": "delete", "path": ["firewall","group",'address-group', group_name]}
  212. result1 = api_set(hostname, cmd)
  213. return result1
  214. def set_firewall_networkgroup_del(hostname, group_name):
  215. cmd = {"op": "delete", "path": ["firewall","group",'network-group', group_name]}
  216. result1 = api_set(hostname, cmd)
  217. return result1
  218. def set_firewall_addressgroup_rangeadd(hostname, group_name, address_start, address_end):
  219. address = str(address_start) + "-" + str(address_end)
  220. cmd = {"op": "set", "path": ["firewall","group",'address-group', group_name, "address", address]}
  221. result1 = api_set(hostname, cmd)
  222. return result1
  223. def set_firewall_addressgroup_description(hostname, group_name, description):
  224. cmd = {"op": "set", "path": ["firewall","group",'address-group', group_name, "description", description]}
  225. result1 = api_set(hostname, cmd)
  226. return result1
  227. def set_firewall_networkgroup_add(hostname, group_name, network):
  228. cmd = {"op": "set", "path": ["firewall","group",'network-group', group_name, "network", network]}
  229. result1 = api_set(hostname, cmd)
  230. return result1
  231. def set_firewall_networkgroup_description(hostname, group_name, description):
  232. cmd = {"op": "set", "path": ["firewall","group",'network-group', group_name, "description", description]}
  233. result1 = api_set(hostname, cmd)
  234. return result1
  235. def delete_route_static(hostname, subnet, nexthop):
  236. #cmd = {"op": "delete", "path": ["protocols","static","route", subnet, "next-hop", nexthop]}
  237. cmd = {"op": "delete", "path": ["protocols","static","route", subnet]}
  238. result1 = api_set(hostname, cmd)
  239. return result1
  240. def delete_route_rule(hostname, firewall_name, rule_name):
  241. cmd = {"op": "delete", "path": ["firewall", "name", firewall_name, "rule", rule_name]}
  242. result1 = api_set(hostname, cmd)
  243. return result1
  244. def delete_firewall(hostname, name):
  245. cmd = {"op": "delete", "path": ["firewall","name", name]}
  246. result1 = api_set(hostname, cmd)
  247. return result1
  248. def ip_route(hostname):
  249. cmd = {"op": "show", "path": ["ip","route"]}
  250. result1 = api_show(hostname, cmd)
  251. return result1