vyos.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 get_hostname_prefered(request):
  33. hostname = None
  34. if request.session.get('hostname', None) != None:
  35. hostname = request.session.get('hostname', None)
  36. if hostname == None:
  37. try:
  38. instance = Instance.objects.get(main=True)
  39. except Instance.DoesNotExist:
  40. return None
  41. hostname = instance.hostname
  42. return hostname
  43. def instance_getall():
  44. instances = Instance.objects.all()
  45. return instances
  46. def conntry(hostname):
  47. cmd = {"op": "showConfig", "path": ["interfaces"]}
  48. print(json.dumps(cmd))
  49. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  50. print(post)
  51. print(get_url_retrieve(hostname))
  52. try:
  53. resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
  54. except requests.exceptions.ConnectionError:
  55. return False
  56. print(resp.status_code)
  57. if (resp.status_code == 200):
  58. return True
  59. pprint.pprint(resp)
  60. pprint.pprint(resp.json())
  61. return False
  62. def getall(hostname="179.127.12.142"):
  63. #cmd = {"op": "save", "file": "/config/config.boot"}
  64. cmd = {"op": "showConfig", "path": ["interfaces", "dummy"]}
  65. print(json.dumps(cmd))
  66. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  67. print(post)
  68. try:
  69. resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
  70. except requests.exceptions.ConnectionError:
  71. return False
  72. print(resp.status_code)
  73. pprint.pprint(resp)
  74. pprint.pprint(resp.json())
  75. if resp.status_code != 200:
  76. # This means something went wrong.
  77. #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
  78. return "erro"
  79. #for todo_item in resp.json():
  80. #print('{} {}'.format(todo_item['id'], todo_item['summary']))
  81. return resp
  82. def get_interfaces(hostname="179.127.12.142"):
  83. cmd = {"op": "showConfig", "path": ["interfaces"]}
  84. print(json.dumps(cmd))
  85. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  86. print(post)
  87. try:
  88. resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
  89. except requests.exceptions.ConnectionError:
  90. return False
  91. print(resp.status_code)
  92. pprint.pprint(resp)
  93. pprint.pprint(resp.json())
  94. if resp.status_code != 200:
  95. # This means something went wrong.
  96. #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
  97. return "erro"
  98. #for todo_item in resp.json():
  99. #print('{} {}'.format(todo_item['id'], todo_item['summary']))
  100. result1 = resp.json()
  101. print(result1['data'])
  102. #result2 = json.loads(result1['data'])
  103. pprint.pprint(result1)
  104. return result1['data']
  105. def get_interface(interface_type, interface_name, hostname):
  106. cmd = {"op": "showConfig", "path": ["interfaces", interface_type, interface_name]}
  107. print(json.dumps(cmd))
  108. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  109. print(post)
  110. try:
  111. resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
  112. except requests.exceptions.ConnectionError:
  113. return False
  114. print(resp.status_code)
  115. pprint.pprint(resp)
  116. pprint.pprint(resp.json())
  117. if resp.status_code != 200:
  118. # This means something went wrong.
  119. #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
  120. return "erro"
  121. #for todo_item in resp.json():
  122. #print('{} {}'.format(todo_item['id'], todo_item['summary']))
  123. result1 = resp.json()
  124. print(result1['data'])
  125. #result2 = json.loads(result1['data'])
  126. pprint.pprint(result1)
  127. return result1['data']
  128. def get_firewall_all(hostname):
  129. cmd = {"op": "showConfig", "path": ["firewall"]}
  130. print(json.dumps(cmd))
  131. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  132. print(post)
  133. try:
  134. resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
  135. except requests.exceptions.ConnectionError:
  136. return False
  137. print(resp.status_code)
  138. pprint.pprint(resp)
  139. pprint.pprint(resp.json())
  140. if resp.status_code != 200:
  141. # This means something went wrong.
  142. #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
  143. return "erro"
  144. #for todo_item in resp.json():
  145. #print('{} {}'.format(todo_item['id'], todo_item['summary']))
  146. result1 = resp.json()
  147. print(result1['data'])
  148. #result2 = json.loads(result1['data'])
  149. pprint.pprint(result1)
  150. return result1['data']
  151. def get_firewall(hostname, name):
  152. cmd = {"op": "showConfig", "path": ["firewall", name]}
  153. print(json.dumps(cmd))
  154. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  155. print(post)
  156. try:
  157. resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
  158. except requests.exceptions.ConnectionError:
  159. return False
  160. print(resp.status_code)
  161. pprint.pprint(resp)
  162. pprint.pprint(resp.json())
  163. if resp.status_code != 200:
  164. # This means something went wrong.
  165. #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
  166. return "erro"
  167. #for todo_item in resp.json():
  168. #print('{} {}'.format(todo_item['id'], todo_item['summary']))
  169. result1 = resp.json()
  170. print(result1['data'])
  171. #result2 = json.loads(result1['data'])
  172. pprint.pprint(result1)
  173. return result1['data']
  174. def set_config(hostname, cmd):
  175. print(json.dumps(cmd))
  176. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  177. print(post)
  178. try:
  179. resp = requests.post(get_url_configure(hostname), verify=False, data=post, timeout=15)
  180. except requests.exceptions.ConnectionError:
  181. return False
  182. print(resp.status_code)
  183. pprint.pprint(resp)
  184. pprint.pprint(resp.json())
  185. if resp.status_code != 200:
  186. # This means something went wrong.
  187. #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
  188. return "erro"
  189. #for todo_item in resp.json():
  190. #print('{} {}'.format(todo_item['id'], todo_item['summary']))
  191. result1 = resp.json()
  192. print(result1['data'])
  193. #result2 = json.loads(result1['data'])
  194. pprint.pprint(result1)
  195. return result1['data']
  196. def insert_firewall_rules(hostname, firewall_name):
  197. cmd = {"op": "set", "path": ["firewall", firewall_name, "rule", request.POST['rulenumber'], "action", request.POST['action']]}
  198. result1 = set_config(hostname, cmd)
  199. #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