123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- import requests
- import json
- import pprint
- import sys
- #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
- sys.path.append('/var/secrets')
- from config.models import Instance
- def get_url(hostname):
- # permcheck
- instance = Instance.objects.get(hostname=hostname)
- if instance.https == True:
- protocol = "https"
- else:
- protocol = "http"
- if (instance.port == None):
- instance.port = 443
- url = protocol + "://" + instance.hostname + ":" + str(instance.port)
- return url
- def get_url_manage(hostname):
- url = get_url(hostname) + '/config-file'
- return url
- def get_url_configure(hostname):
- url = get_url(hostname) + '/configure'
- return url
- def get_url_retrieve(hostname):
- url = get_url(hostname) + '/retrieve'
- return url
- def get_key(hostname):
- # permcheck
- instance = Instance.objects.get(hostname=hostname)
- return instance.key
- def get_hostname_prefered(request):
- hostname = None
- if request.session.get('hostname', None) != None:
- hostname = request.session.get('hostname', None)
-
- if hostname == None:
- try:
- instance = Instance.objects.get(main=True)
- except Instance.DoesNotExist:
- return None
- hostname = instance.hostname
- return hostname
-
- def instance_getall():
- instances = Instance.objects.all()
- return instances
- def conntry(hostname):
- cmd = {"op": "showConfig", "path": ["interfaces"]}
- print(json.dumps(cmd))
- post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
- print(post)
-
- print(get_url_retrieve(hostname))
- try:
- resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
- except requests.exceptions.ConnectionError:
- return False
-
- print(resp.status_code)
- if (resp.status_code == 200):
- return True
-
- pprint.pprint(resp)
- pprint.pprint(resp.json())
- return False
- def getall(hostname="179.127.12.142"):
- #cmd = {"op": "save", "file": "/config/config.boot"}
- cmd = {"op": "showConfig", "path": ["interfaces", "dummy"]}
- print(json.dumps(cmd))
- post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
- print(post)
- try:
- resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
- except requests.exceptions.ConnectionError:
- return False
- print(resp.status_code)
- pprint.pprint(resp)
- pprint.pprint(resp.json())
- if resp.status_code != 200:
- # This means something went wrong.
- #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
- return "erro"
- #for todo_item in resp.json():
- #print('{} {}'.format(todo_item['id'], todo_item['summary']))
- return resp
- def get_interfaces(hostname="179.127.12.142"):
- cmd = {"op": "showConfig", "path": ["interfaces"]}
- print(json.dumps(cmd))
- post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
- print(post)
- try:
- resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
- except requests.exceptions.ConnectionError:
- return False
- print(resp.status_code)
- pprint.pprint(resp)
- pprint.pprint(resp.json())
- if resp.status_code != 200:
- # This means something went wrong.
- #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
- return "erro"
- #for todo_item in resp.json():
- #print('{} {}'.format(todo_item['id'], todo_item['summary']))
- result1 = resp.json()
- print(result1['data'])
- #result2 = json.loads(result1['data'])
- pprint.pprint(result1)
- return result1['data']
- def get_interface(interface_type, interface_name, hostname):
- cmd = {"op": "showConfig", "path": ["interfaces", interface_type, interface_name]}
- print(json.dumps(cmd))
- post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
- print(post)
- try:
- resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
- except requests.exceptions.ConnectionError:
- return False
- print(resp.status_code)
- pprint.pprint(resp)
- pprint.pprint(resp.json())
- if resp.status_code != 200:
- # This means something went wrong.
- #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
- return "erro"
- #for todo_item in resp.json():
- #print('{} {}'.format(todo_item['id'], todo_item['summary']))
- result1 = resp.json()
- print(result1['data'])
- #result2 = json.loads(result1['data'])
- pprint.pprint(result1)
- return result1['data']
- def get_firewall_all(hostname):
- cmd = {"op": "showConfig", "path": ["firewall"]}
- print(json.dumps(cmd))
- post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
- print(post)
- try:
- resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
- except requests.exceptions.ConnectionError:
- return False
- print(resp.status_code)
- pprint.pprint(resp)
- pprint.pprint(resp.json())
- if resp.status_code != 200:
- # This means something went wrong.
- #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
- return "erro"
- #for todo_item in resp.json():
- #print('{} {}'.format(todo_item['id'], todo_item['summary']))
- result1 = resp.json()
- print(result1['data'])
- #result2 = json.loads(result1['data'])
- pprint.pprint(result1)
- return result1['data']
- def get_firewall(hostname, name):
- cmd = {"op": "showConfig", "path": ["firewall", name]}
- print(json.dumps(cmd))
- post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
- print(post)
- try:
- resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
- except requests.exceptions.ConnectionError:
- return False
- print(resp.status_code)
- pprint.pprint(resp)
- pprint.pprint(resp.json())
- if resp.status_code != 200:
- # This means something went wrong.
- #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
- return "erro"
- #for todo_item in resp.json():
- #print('{} {}'.format(todo_item['id'], todo_item['summary']))
- result1 = resp.json()
- print(result1['data'])
- #result2 = json.loads(result1['data'])
- pprint.pprint(result1)
- return result1['data']
- def set_config(hostname, cmd):
- print(json.dumps(cmd))
- post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
- print(post)
- try:
- resp = requests.post(get_url_configure(hostname), verify=False, data=post, timeout=15)
- except requests.exceptions.ConnectionError:
- return False
- print(resp.status_code)
- pprint.pprint(resp)
- pprint.pprint(resp.json())
- if resp.status_code != 200:
- # This means something went wrong.
- #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
- return "erro"
- #for todo_item in resp.json():
- #print('{} {}'.format(todo_item['id'], todo_item['summary']))
- result1 = resp.json()
- print(result1['data'])
- #result2 = json.loads(result1['data'])
- pprint.pprint(result1)
- return result1['data']
- def insert_firewall_rules(hostname, firewall_name):
- cmd = {"op": "set", "path": ["firewall", firewall_name, "rule", request.POST['rulenumber'], "action", request.POST['action']]}
- result1 = set_config(hostname, cmd)
- #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
|