vyos.py 7.5 KB

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