vyos.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 get_interfaces(hostname):
  109. cmd = {"op": "showConfig", "path": ["interfaces"]}
  110. result1 = api_get(hostname, cmd)
  111. return result1
  112. def get_interfaces_all_names(hostname):
  113. interfaces = get_interfaces(hostname)
  114. all_names = []
  115. for itype in interfaces:
  116. for iname in interfaces[itype]:
  117. all_names.append({
  118. 'interface_name': iname,
  119. 'type': itype
  120. })
  121. if 'vif' in interfaces[itype][iname]:
  122. for vif in interfaces[itype][iname]['vif']:
  123. all_names.append({
  124. 'interface_name': iname,
  125. 'type': itype,
  126. 'vif': vif
  127. })
  128. return all_names
  129. def get_interface(interface_type, interface_name, hostname):
  130. cmd = {"op": "showConfig", "path": ["interfaces", interface_type, interface_name]}
  131. result1 = api_get(hostname, cmd)
  132. return result1
  133. def get_firewall(hostname, name):
  134. cmd = {"op": "showConfig", "path": ["firewall", "name", name]}
  135. result1 = api_get(hostname, cmd)
  136. return result1
  137. def get_firewall_rule(hostname, name, rulenumber):
  138. cmd = {"op": "showConfig", "path": ["firewall", "name", name, "rule", rulenumber]}
  139. result1 = api_get(hostname, cmd)
  140. return result1
  141. def set_config(hostname, cmd):
  142. #cmd = {"op": "set", "path": ["interface", interface_type, interface_name, "firewall", direction, "name", firewall_name]}
  143. result1 = api_set(hostname, cmd)
  144. return result1
  145. def insert_firewall_rules(hostname, cmd):
  146. pprint.pprint(cmd)
  147. result1 = api_set(hostname, cmd)
  148. return result1
  149. def get_route_static(hostname):
  150. cmd = {"op": "showConfig", "path": ["protocols","static","route"]}
  151. result1 = api_get(hostname, cmd)
  152. return result1
  153. def set_firewall_syncookies_enable(hostname):
  154. cmd = {"op": "set", "path": ["firewall","syn-cookies",'enable']}
  155. result1 = api_set(hostname, cmd)
  156. return result1
  157. def set_firewall_syncookies_disable(hostname):
  158. cmd = {"op": "set", "path": ["firewall","syn-cookies",'disable']}
  159. result1 = api_set(hostname, cmd)
  160. return result1
  161. def set_firewall_allping_enable(hostname):
  162. cmd = {"op": "set", "path": ["firewall","all-ping",'enable']}
  163. result1 = api_set(hostname, cmd)
  164. return result1
  165. def set_firewall_allping_disable(hostname):
  166. cmd = {"op": "set", "path": ["firewall","all-ping",'disable']}
  167. result1 = api_set(hostname, cmd)
  168. return result1
  169. def get_firewall_portgroup(hostname):
  170. cmd = {"op": "showConfig", "path": ["firewall","group","port-group"]}
  171. result1 = api_get(hostname, cmd)
  172. return result1
  173. def set_firewall_portgroup_del(hostname, group_name):
  174. cmd = {"op": "delete", "path": ["firewall","group",'port-group', group_name]}
  175. result1 = api_set(hostname, cmd)
  176. return result1
  177. def set_firewall_portgroup_description(hostname, group_name, description):
  178. cmd = {"op": "set", "path": ["firewall","group",'port-group', group_name, "description", description]}
  179. result1 = api_set(hostname, cmd)
  180. return result1
  181. def set_firewall_portgroup_add(hostname, group_name, port):
  182. cmd = {"op": "set", "path": ["firewall","group",'port-group', group_name, "port", port]}
  183. result1 = api_set(hostname, cmd)
  184. return result1
  185. def set_firewall_portgroup_delete_port(hostname, group_name, port):
  186. cmd = {"op": "delete", "path": ["firewall","group",'port-group', group_name, "port", port]}
  187. result1 = api_set(hostname, cmd)
  188. return result1
  189. def get_firewall_addressgroup(hostname):
  190. cmd = {"op": "showConfig", "path": ["firewall","group","address-group"]}
  191. result1 = api_get(hostname, cmd)
  192. return result1
  193. def get_firewall_networkgroup(hostname):
  194. cmd = {"op": "showConfig", "path": ["firewall","group","network-group"]}
  195. result1 = api_get(hostname, cmd)
  196. return result1
  197. def get_firewall_addressgroup_one(hostname, group_name):
  198. cmd = {"op": "showConfig", "path": ["firewall","group","address-group", group_name]}
  199. result1 = api_get(hostname, cmd)
  200. return result1
  201. def get_firewall_networkgroup_one(hostname, group_name):
  202. cmd = {"op": "showConfig", "path": ["firewall","group","network-group", group_name]}
  203. result1 = api_get(hostname, cmd)
  204. return result1
  205. def set_firewall_networkgroup_description(hostname, group_name, description):
  206. cmd = {"op": "set", "path": ["firewall","group",'network-group', group_name, "description", description]}
  207. result1 = api_set(hostname, cmd)
  208. return result1
  209. def set_firewall_addressgroup_description(hostname, group_name, description):
  210. cmd = {"op": "set", "path": ["firewall","group",'address-group', group_name, "description", description]}
  211. result1 = api_set(hostname, cmd)
  212. return result1
  213. def set_firewall_addressgroup_add(hostname, group_name, address):
  214. cmd = {"op": "set", "path": ["firewall","group",'address-group', group_name, "address", address]}
  215. result1 = api_set(hostname, cmd)
  216. return result1
  217. def set_firewall_addressgroup_del(hostname, group_name):
  218. cmd = {"op": "delete", "path": ["firewall","group",'address-group', group_name]}
  219. result1 = api_set(hostname, cmd)
  220. return result1
  221. def set_firewall_networkgroup_del(hostname, group_name):
  222. cmd = {"op": "delete", "path": ["firewall","group",'network-group', group_name]}
  223. result1 = api_set(hostname, cmd)
  224. return result1
  225. def set_firewall_addressgroup_rangeadd(hostname, group_name, address_start, address_end):
  226. address = str(address_start) + "-" + str(address_end)
  227. cmd = {"op": "set", "path": ["firewall","group",'address-group', group_name, "address", address]}
  228. result1 = api_set(hostname, cmd)
  229. return result1
  230. def set_firewall_addressgroup_description(hostname, group_name, description):
  231. cmd = {"op": "set", "path": ["firewall","group",'address-group', group_name, "description", description]}
  232. result1 = api_set(hostname, cmd)
  233. return result1
  234. def set_firewall_networkgroup_add(hostname, group_name, network):
  235. cmd = {"op": "set", "path": ["firewall","group",'network-group', group_name, "network", network]}
  236. result1 = api_set(hostname, cmd)
  237. return result1
  238. def set_firewall_networkgroup_description(hostname, group_name, description):
  239. cmd = {"op": "set", "path": ["firewall","group",'network-group', group_name, "description", description]}
  240. result1 = api_set(hostname, cmd)
  241. return result1
  242. def delete_route_static(hostname, subnet, nexthop):
  243. #cmd = {"op": "delete", "path": ["protocols","static","route", subnet, "next-hop", nexthop]}
  244. cmd = {"op": "delete", "path": ["protocols","static","route", subnet]}
  245. result1 = api_set(hostname, cmd)
  246. return result1
  247. def delete_route_rule(hostname, firewall_name, rule_name):
  248. cmd = {"op": "delete", "path": ["firewall", "name", firewall_name, "rule", rule_name]}
  249. result1 = api_set(hostname, cmd)
  250. return result1
  251. def delete_firewall(hostname, name):
  252. cmd = {"op": "delete", "path": ["firewall","name", name]}
  253. result1 = api_set(hostname, cmd)
  254. return result1
  255. def ip_route(hostname):
  256. cmd = {"op": "show", "path": ["ip","route"]}
  257. result1 = api_show(hostname, cmd)
  258. return result1