vyos.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #data='{"op": "showConfig", "path": ["interfaces", "dummy"]}
  34. def instance_getall():
  35. instances = Instance.objects.all()
  36. return instances
  37. def conntry(hostname):
  38. cmd = {"op": "showConfig", "path": ["interfaces"]}
  39. print(json.dumps(cmd))
  40. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  41. print(post)
  42. # curl -X POST -F data='{"op": "showConfig", "path": ["interfaces", "dummy"]}' -F key=qwerty http://127.0.0.1:8080/retrieve
  43. # {"success": true, "data": " /* So very dummy */\n dummy dum0 {\n address 192.168.168.1/32\n address 192.168.168.2/32\n /* That is a description */\n description \"Test interface\"\n }\n dummy dum1 {\n address 203.0.113.76/32\n address 203.0.113.79/32\n }\n", "error": null}
  44. print(get_url_retrieve(hostname))
  45. try:
  46. resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
  47. except requests.exceptions.ConnectionError:
  48. return False
  49. print(resp.status_code)
  50. if (resp.status_code == 200):
  51. return True
  52. pprint.pprint(resp)
  53. pprint.pprint(resp.json())
  54. return False
  55. def getall(hostname="179.127.12.142"):
  56. #cmd = {"op": "save", "file": "/config/config.boot"}
  57. cmd = {"op": "showConfig", "path": ["interfaces", "dummy"]}
  58. print(json.dumps(cmd))
  59. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  60. print(post)
  61. # curl -X POST -F data='{"op": "showConfig", "path": ["interfaces", "dummy"]}' -F key=qwerty http://127.0.0.1:8080/retrieve
  62. # {"success": true, "data": " /* So very dummy */\n dummy dum0 {\n address 192.168.168.1/32\n address 192.168.168.2/32\n /* That is a description */\n description \"Test interface\"\n }\n dummy dum1 {\n address 203.0.113.76/32\n address 203.0.113.79/32\n }\n", "error": null}
  63. try:
  64. resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
  65. except requests.exceptions.ConnectionError:
  66. return False
  67. print(resp.status_code)
  68. pprint.pprint(resp)
  69. pprint.pprint(resp.json())
  70. if resp.status_code != 200:
  71. # This means something went wrong.
  72. #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
  73. return "erro"
  74. #for todo_item in resp.json():
  75. #print('{} {}'.format(todo_item['id'], todo_item['summary']))
  76. return resp
  77. def get_interfaces(hostname="179.127.12.142"):
  78. cmd = {"op": "showConfig", "path": ["interfaces"]}
  79. print(json.dumps(cmd))
  80. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  81. print(post)
  82. # curl -X POST -F data='{"op": "showConfig", "path": ["interfaces", "dummy"]}' -F key=qwerty http://127.0.0.1:8080/retrieve
  83. # {"success": true, "data": " /* So very dummy */\n dummy dum0 {\n address 192.168.168.1/32\n address 192.168.168.2/32\n /* That is a description */\n description \"Test interface\"\n }\n dummy dum1 {\n address 203.0.113.76/32\n address 203.0.113.79/32\n }\n", "error": null}
  84. try:
  85. resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
  86. except requests.exceptions.ConnectionError:
  87. return False
  88. print(resp.status_code)
  89. pprint.pprint(resp)
  90. pprint.pprint(resp.json())
  91. if resp.status_code != 200:
  92. # This means something went wrong.
  93. #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
  94. return "erro"
  95. #for todo_item in resp.json():
  96. #print('{} {}'.format(todo_item['id'], todo_item['summary']))
  97. result1 = resp.json()
  98. print(result1['data'])
  99. #result2 = json.loads(result1['data'])
  100. pprint.pprint(result1)
  101. return result1['data']
  102. def get_interface(interface_type, interface_name, hostname="179.127.12.142"):
  103. cmd = {"op": "showConfig", "path": ["interfaces", interface_type, interface_name]}
  104. print(json.dumps(cmd))
  105. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  106. print(post)
  107. # curl -X POST -F data='{"op": "showConfig", "path": ["interfaces", "dummy"]}' -F key=qwerty http://127.0.0.1:8080/retrieve
  108. # {"success": true, "data": " /* So very dummy */\n dummy dum0 {\n address 192.168.168.1/32\n address 192.168.168.2/32\n /* That is a description */\n description \"Test interface\"\n }\n dummy dum1 {\n address 203.0.113.76/32\n address 203.0.113.79/32\n }\n", "error": null}
  109. try:
  110. resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
  111. except requests.exceptions.ConnectionError:
  112. return False
  113. print(resp.status_code)
  114. pprint.pprint(resp)
  115. pprint.pprint(resp.json())
  116. if resp.status_code != 200:
  117. # This means something went wrong.
  118. #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
  119. return "erro"
  120. #for todo_item in resp.json():
  121. #print('{} {}'.format(todo_item['id'], todo_item['summary']))
  122. result1 = resp.json()
  123. print(result1['data'])
  124. #result2 = json.loads(result1['data'])
  125. pprint.pprint(result1)
  126. return result1['data']