vyos.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import requests
  2. import json
  3. import pprint
  4. import sys
  5. from config.models import Instance
  6. def get_url(hostname):
  7. # permcheck
  8. instance = Instance.objects.get(hostname=hostname)
  9. if instance.https == True:
  10. protocol = "https"
  11. else:
  12. protocol = "http"
  13. if (instance.port == None):
  14. instance.port = 443
  15. url = protocol + "://" + instance.hostname + ":" + str(instance.port)
  16. return url
  17. def get_url_manage(hostname):
  18. url = get_url(hostname) + '/config-file'
  19. return url
  20. def get_url_configure(hostname):
  21. url = get_url(hostname) + '/configure'
  22. return url
  23. def get_url_retrieve(hostname):
  24. url = get_url(hostname) + '/retrieve'
  25. return url
  26. def get_key(hostname):
  27. # permcheck
  28. instance = Instance.objects.get(hostname=hostname)
  29. return instance.key
  30. def api(type, hostname, cmd):
  31. if type == "retrieve":
  32. url = get_url_retrieve(hostname)
  33. elif type == "manage":
  34. url = get_url_manage(hostname)
  35. elif type == "configure":
  36. url = get_url_configure(hostname)
  37. else:
  38. return False
  39. print(json.dumps(cmd))
  40. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  41. print(post)
  42. try:
  43. resp = requests.post(url, verify=False, data=post, timeout=5)
  44. except requests.exceptions.ConnectionError:
  45. return False
  46. print(resp.status_code)
  47. pprint.pprint(resp)
  48. pprint.pprint(resp.json())
  49. if resp.status_code != 200:
  50. # This means something went wrong.
  51. #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
  52. return False
  53. #for todo_item in resp.json():
  54. #print('{} {}'.format(todo_item['id'], todo_item['summary']))
  55. result1 = resp.json()
  56. print(result1['data'])
  57. #result2 = json.loads(result1['data'])
  58. pprint.pprint(result1)
  59. return result1['data']
  60. def api_get(hostname, cmd):
  61. return api('retrieve', hostname, cmd)
  62. def api_set(hostname, cmd):
  63. return api('configure', hostname, cmd)
  64. def get_hostname_prefered(request):
  65. hostname = None
  66. if request.session.get('hostname', None) != None:
  67. hostname = request.session.get('hostname', None)
  68. if hostname == None:
  69. try:
  70. instance = Instance.objects.get(main=True)
  71. except Instance.DoesNotExist:
  72. return None
  73. hostname = instance.hostname
  74. return hostname
  75. def conntry(hostname):
  76. cmd = {"op": "showConfig", "path": ["interfaces"]}
  77. print(json.dumps(cmd))
  78. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  79. print(post)
  80. print(get_url_retrieve(hostname))
  81. try:
  82. resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
  83. except requests.exceptions.ConnectionError:
  84. return False
  85. print(resp.status_code)
  86. if (resp.status_code == 200):
  87. return True
  88. pprint.pprint(resp)
  89. pprint.pprint(resp.json())
  90. return False
  91. def instance_getall():
  92. instances = Instance.objects.all()
  93. return instances
  94. def get_firewall_all(hostname):
  95. cmd = {"op": "showConfig", "path": ["firewall"]}
  96. firewall_list = api_get(hostname, cmd)
  97. return firewall_list
  98. def set_interface_firewall_ipv4(hostname, interface_type, interface_name, direction, firewall_name):
  99. cmd = {"op": "set", "path": ["interfaces", interface_type, interface_name, "firewall", direction, "name", firewall_name]}
  100. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  101. success = api_set(hostname, cmd)
  102. return success
  103. def get_interfaces(hostname):
  104. cmd = {"op": "showConfig", "path": ["interfaces"]}
  105. result1 = api_get(hostname, cmd)
  106. return result1
  107. def get_interface(interface_type, interface_name, hostname):
  108. cmd = {"op": "showConfig", "path": ["interfaces", interface_type, interface_name]}
  109. result1 = api_get(hostname, cmd)
  110. return result1
  111. def get_firewall(hostname, name):
  112. cmd = {"op": "showConfig", "path": ["firewall", "name", name]}
  113. result1 = api_get(hostname, cmd)
  114. return result1
  115. def get_firewall_rule(hostname, name, rulenumber):
  116. cmd = {"op": "showConfig", "path": ["firewall", "name", name, "rule", rulenumber]}
  117. result1 = api_get(hostname, cmd)
  118. return result1
  119. def set_config(hostname, cmd):
  120. #cmd = {"op": "set", "path": ["interface", interface_type, interface_name, "firewall", direction, "name", firewall_name]}
  121. result1 = api_set(hostname, cmd)
  122. return result1
  123. def insert_firewall_rules(hostname, cmd):
  124. pprint.pprint(cmd)
  125. result1 = api_set(hostname, cmd)
  126. return result1
  127. def get_static(hostname):
  128. cmd = {"op": "showConfig", "path": ["show","ip","route","static"]}
  129. result1 = api_get(hostname, cmd)
  130. return result1