vyos.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. import requests
  2. import json
  3. import pprint
  4. import sys
  5. #curl -k -X POST -F data='{"op": "set", "path": ["interfaces", "dummy", "dum1", "address"], "value": "203.0.113.76/32"}' -F key=a6ffb742a8a631a65b07ab2026258629da2632fd https://179.127.12.142:44302/configure
  6. sys.path.append('/var/secrets')
  7. from config.models import Instance
  8. def get_url(hostname):
  9. # permcheck
  10. instance = Instance.objects.get(hostname=hostname)
  11. if instance.https == True:
  12. protocol = "https"
  13. else:
  14. protocol = "http"
  15. if (instance.port == None):
  16. instance.port = 443
  17. url = protocol + "://" + instance.hostname + ":" + str(instance.port)
  18. return url
  19. def get_url_manage(hostname):
  20. url = get_url(hostname) + '/config-file'
  21. return url
  22. def get_url_configure(hostname):
  23. url = get_url(hostname) + '/configure'
  24. return url
  25. def get_url_retrieve(hostname):
  26. url = get_url(hostname) + '/retrieve'
  27. return url
  28. def get_key(hostname):
  29. # permcheck
  30. instance = Instance.objects.get(hostname=hostname)
  31. return instance.key
  32. def api_get(type, hostname, cmd):
  33. if type == "retrieve":
  34. url = get_url_retrieve(hostname)
  35. elif type == "manage":
  36. url = get_url_manage(hostname)
  37. elif type == "configure":
  38. url = get_url_configure(hostname)
  39. else:
  40. return False
  41. print(json.dumps(cmd))
  42. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  43. print(post)
  44. try:
  45. resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=5)
  46. except requests.exceptions.ConnectionError:
  47. return False
  48. print(resp.status_code)
  49. pprint.pprint(resp)
  50. pprint.pprint(resp.json())
  51. if resp.status_code != 200:
  52. # This means something went wrong.
  53. #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
  54. return False
  55. #for todo_item in resp.json():
  56. #print('{} {}'.format(todo_item['id'], todo_item['summary']))
  57. result1 = resp.json()
  58. print(result1['data'])
  59. #result2 = json.loads(result1['data'])
  60. pprint.pprint(result1)
  61. return result1['data']
  62. def get_hostname_prefered(request):
  63. hostname = None
  64. if request.session.get('hostname', None) != None:
  65. hostname = request.session.get('hostname', None)
  66. if hostname == None:
  67. try:
  68. instance = Instance.objects.get(main=True)
  69. except Instance.DoesNotExist:
  70. return None
  71. hostname = instance.hostname
  72. return hostname
  73. def conntry(hostname):
  74. cmd = {"op": "showConfig", "path": ["interfaces"]}
  75. print(json.dumps(cmd))
  76. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  77. print(post)
  78. print(get_url_retrieve(hostname))
  79. try:
  80. resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
  81. except requests.exceptions.ConnectionError:
  82. return False
  83. print(resp.status_code)
  84. if (resp.status_code == 200):
  85. return True
  86. pprint.pprint(resp)
  87. pprint.pprint(resp.json())
  88. return False
  89. def instance_getall():
  90. instances = Instance.objects.all()
  91. return instances
  92. def get_firewall_all(hostname):
  93. cmd = {"op": "showConfig", "path": ["firewall"]}
  94. firewall_list = api_get("retrieve", hostname, cmd)
  95. return firewall_list
  96. def getall(hostname="179.127.12.142"):
  97. #cmd = {"op": "save", "file": "/config/config.boot"}
  98. cmd = {"op": "showConfig", "path": ["interfaces", "dummy"]}
  99. print(json.dumps(cmd))
  100. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  101. print(post)
  102. try:
  103. resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
  104. except requests.exceptions.ConnectionError:
  105. return False
  106. print(resp.status_code)
  107. pprint.pprint(resp)
  108. pprint.pprint(resp.json())
  109. if resp.status_code != 200:
  110. # This means something went wrong.
  111. #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
  112. return "erro"
  113. #for todo_item in resp.json():
  114. #print('{} {}'.format(todo_item['id'], todo_item['summary']))
  115. return resp
  116. def get_interfaces(hostname="179.127.12.142"):
  117. cmd = {"op": "showConfig", "path": ["interfaces"]}
  118. print(json.dumps(cmd))
  119. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  120. print(post)
  121. try:
  122. resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
  123. except requests.exceptions.ConnectionError:
  124. return False
  125. print(resp.status_code)
  126. pprint.pprint(resp)
  127. pprint.pprint(resp.json())
  128. if resp.status_code != 200:
  129. # This means something went wrong.
  130. #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
  131. return "erro"
  132. #for todo_item in resp.json():
  133. #print('{} {}'.format(todo_item['id'], todo_item['summary']))
  134. result1 = resp.json()
  135. print(result1['data'])
  136. #result2 = json.loads(result1['data'])
  137. pprint.pprint(result1)
  138. return result1['data']
  139. def get_interface(interface_type, interface_name, hostname):
  140. cmd = {"op": "showConfig", "path": ["interfaces", interface_type, interface_name]}
  141. print(json.dumps(cmd))
  142. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  143. print(post)
  144. try:
  145. resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
  146. except requests.exceptions.ConnectionError:
  147. return False
  148. print(resp.status_code)
  149. pprint.pprint(resp)
  150. pprint.pprint(resp.json())
  151. if resp.status_code != 200:
  152. # This means something went wrong.
  153. #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
  154. return "erro"
  155. #for todo_item in resp.json():
  156. #print('{} {}'.format(todo_item['id'], todo_item['summary']))
  157. result1 = resp.json()
  158. print(result1['data'])
  159. #result2 = json.loads(result1['data'])
  160. pprint.pprint(result1)
  161. return result1['data']
  162. def get_firewall(hostname, name):
  163. cmd = {"op": "showConfig", "path": ["firewall", "name", name]}
  164. print(json.dumps(cmd))
  165. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  166. print(post)
  167. try:
  168. resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
  169. except requests.exceptions.ConnectionError:
  170. return False
  171. print(resp.status_code)
  172. pprint.pprint(resp)
  173. pprint.pprint(resp.json())
  174. if resp.status_code != 200:
  175. # This means something went wrong.
  176. #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
  177. return "erro"
  178. #for todo_item in resp.json():
  179. #print('{} {}'.format(todo_item['id'], todo_item['summary']))
  180. result1 = resp.json()
  181. print(result1['data'])
  182. #result2 = json.loads(result1['data'])
  183. pprint.pprint(result1)
  184. return result1['data']
  185. def get_firewall_rule(hostname, name, rulenumber):
  186. cmd = {"op": "showConfig", "path": ["firewall", "name", name, "rule", rulenumber]}
  187. print(json.dumps(cmd))
  188. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  189. print(post)
  190. try:
  191. resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
  192. except requests.exceptions.ConnectionError:
  193. return False
  194. print(resp.status_code)
  195. pprint.pprint(resp)
  196. pprint.pprint(resp.json())
  197. if resp.status_code != 200:
  198. # This means something went wrong.
  199. #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
  200. return "erro"
  201. #for todo_item in resp.json():
  202. #print('{} {}'.format(todo_item['id'], todo_item['summary']))
  203. result1 = resp.json()
  204. print(result1['data'])
  205. #result2 = json.loads(result1['data'])
  206. pprint.pprint(result1)
  207. return result1['data']
  208. def set_config(hostname, cmd):
  209. print(json.dumps(cmd))
  210. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  211. print(post)
  212. try:
  213. resp = requests.post(get_url_configure(hostname), verify=False, data=post, timeout=15)
  214. except requests.exceptions.ConnectionError:
  215. return False
  216. print(resp.status_code)
  217. pprint.pprint(resp)
  218. pprint.pprint(resp.json())
  219. if resp.status_code != 200:
  220. # This means something went wrong.
  221. #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
  222. return "erro"
  223. #for todo_item in resp.json():
  224. #print('{} {}'.format(todo_item['id'], todo_item['summary']))
  225. result1 = resp.json()
  226. print(result1['data'])
  227. #result2 = json.loads(result1['data'])
  228. pprint.pprint(result1)
  229. return result1['data']
  230. def insert_firewall_rules(hostname, firewall_name):
  231. cmd = {"op": "set", "path": ["firewall", firewall_name, "rule", request.POST['rulenumber'], "action", request.POST['action']]}
  232. result1 = set_config(hostname, cmd)
  233. #curl -k -X POST -F data='{"op": "set", "path": ["interfaces", "dummy", "dum1", "address"], "value": "203.0.113.76/32"}' -F key=a6ffb742a8a631a65b07ab2026258629da2632fd https://179.127.12.142:44302/configure