-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfw_api_test.py
155 lines (128 loc) · 4.91 KB
/
fw_api_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python
import pprint
import requests
import getpass
class fortigate_api:
_secure=True
def __init__(self, ip, un, pw, verify=False, proxies=None, disable_warnings=True, secure=True):
if disable_warnings:
requests.packages.urllib3.disable_warnings()
unpw = {'username':un,'secretkey':pw}
self._secure=secure
self.verify = verify
self.ip = ip
self.proxies=proxies
if self._secure:
http='https://'
else:
http='http://'
auth = requests.post(http+self.ip+'/logincheck', data=unpw, verify=self.verify, proxies=self.proxies)
self.cookies = auth.cookies
for cookie in self.cookies:
if cookie.name == "ccsrftoken":
csrftoken = cookie.value[1:-1] # token stored as a list
self.header = {"X-CSRFTOKEN": csrftoken}
def __enter__(self):
return self
def __del__(self):
if self._secure:
http='https://'
else:
http='http://'
try:
requests.post(http+self.ip+'/logout', verify=self.verify, cookies=self.cookies, proxies=self.proxies)
except AttributeError:
print ("Looks like connection to "+self.ip+" has never been established")
def __exit__(self, *args):
pass
def get(self, path, api='v2', params=None):
if isinstance(path, list):
path = '/'.join(path) + '/'
if self._secure:
http='https://'
else:
http='http://'
return requests.get(http+self.ip+'/api/'+api+'/'+path, cookies=self.cookies, verify=self.verify, proxies=self.proxies, params=params)
def put(self, path, api='v2', params=None, data=None):
if isinstance(path, list):
path = '/'.join(path) + '/'
if self._secure:
http='https://'
else:
http='http://'
return requests.put(http+self.ip+'/api/'+api+'/'+path, headers=self.header,cookies=self.cookies,
verify=self.verify, proxies=self.proxies, params=params, json={'json': data})
def post(self, path, api='v2', params=None, data=None, files=None):
if isinstance(path, list):
path = '/'.join(path) + '/'
if self._secure:
http='https://'
else:
http='http://'
return requests.post(http+self.ip+'/api/'+api+'/'+path, headers=self.header,cookies=self.cookies,
verify=self.verify, proxies=self.proxies, params=params, json={'json': data},
files=files)
def delete(self, path, api='v2', params=None, data=None):
if isinstance(path, list):
path = '/'.join(path) + '/'
if self._secure:
http='https://'
else:
http='http://'
return requests.delete(http+self.ip+'/api/'+api+'/'+path, headers=self.header,cookies=self.cookies,
verify=self.verify, proxies=self.proxies, params=params, json={'json': data})
def show(self, path, api='v2', params=None):
response = self.get(path, api=api, params=params)
return response.json()
def edit(self, path, api='v2', params=None, data=None):
response = self.put(path, api=api, params=params, data=data)
return response.json()
def create(self, path, api='v2', params=None, data=None, files=None):
response = self.post(path, api=api, params=params, data=data, files=files)
return response.json()
def remove(self, path, api='v2', params=None, data=None):
response = self.delete(path, api=api, params=params, data=data)
return response.json()
@staticmethod
def print_data(response, verbose=False):
if response['status']=='success':
if verbose:
pprint.pprint (response)
elif response['http_method']=='GET':
pprint.pprint (response['results'])
else:
print ('OK!')
else:
print ('Fail!')
pprint.pprint (response)
def main():
un = "admin"
pw = getpass.getpass()
#proxy={'http': 'socks5://127.0.0.1:9000'}
proxy = None
fw = '192.168.3.19'
intf = {
"name": "testagg",
"vdom": "root",
"allowaccess": "ping",
"ip": "1.1.1.1 255.255.255.0",
"role": "lan",
"type": "aggregate",
"member": [
{
"interface-name": "port6"
},
{
"interface-name": "port7"
}
]
}
try:
t = fortigate_api(fw, un, pw, proxies=proxy)
t.print_data (t.show('cmdb/system/interface'))
t.print_data (t.create('cmdb/system/interface', data=intf))
except:
print ('something went wrong')
raise
if __name__ == "__main__":
main()