vyos.py 7.6 KB

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