-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetonix_api.py
executable file
·238 lines (210 loc) · 7.69 KB
/
netonix_api.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env python3
"""
Python class to access Netonix® WISP Switch WebAPI
** NEITHER THIS CODE NOR THE AUTHOR IS ASSOCIATED WITH NETONIX® IN ANY WAY.**
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
"""
import requests
from requests.exceptions import Timeout
from copy import deepcopy
import time
import json
try:
from deepdiff import DeepDiff
DIFF = True
except:
DIFF = False
class Netonix():
def __init__(self):
self.ip = None
self.s = None
self.url = {}
self.url["login"] = "/index.php"
self.url["backup"] = "/api/v1/backup"
self.url["config"] = "/api/v1/config"
self.url["apply"] = "/api/v1/apply"
self.url["confirm"] = "/api/v1/applystatus"
self.url["reboot"] = "/api/v1/reboot"
self.url["restore"] = "/api/v1/restore"
self.url["mac"] = "/api/v1/mactable"
self.url["status"] = "/api/v1/status/30sec"
self.url["id"] = "/api/v1/bootid"
self.url["update"] = "/api/v1/uploadfirmware"
self.url["doupdate"] = "/api/v1/upgradefirmware"
self.config = {}
self.orig_config = None
self.mac = {}
self.status = {}
self.id = ""
def _get(self, url, params=None, timeout=15, **kwargs):
full_url = "https://"+self.ip+self.url[url]
return self.s.get(full_url, params=params, timeout=timeout, **kwargs)
def _post(self, url, data=None, json=None, timeout=15, **kwargs):
full_url = "https://"+self.ip+self.url[url]
return self.s.post(
full_url,
data=data,
json=json,
timeout=timeout,
**kwargs
)
@staticmethod
def _merge_by_key(old, new, key="Number", append=True):
for item in new:
found = False
for old_item in old:
if(key not in old_item):
continue
if(old_item[key] != item[key]):
continue
old_item.update(item)
found = True
break
if(found is False):
if(append is True):
old_item.append(new)
else:
raise LookupError()
def open(self, ip, user, password):
self.ip = ip
self.s = requests.session()
self.s.verify = False
data = {}
data["username"] = user
data["password"] = password
r = self._post("login", data)
if("Invalid username or password" in r.text):
raise Exception("Invalid username or password")
def getConfig(self):
r = self._get("config")
result = r.json()
if("Config_Version" in result):
self.config = result
def putConfig(self):
r = self._post("config", json=self.config)
try:
r = self._post("apply")
except Timeout:
pass
self.ip = self.config["IPv4_Address"]
for a in range(5):
try:
r = self._post("confirm")
except Timeout:
continue
break
if(r.status_code != requests.codes.ok):
raise Exception("Config Confirm Request Failed")
# return r.json()
def backup(self, output):
r = self.s.get("https://"+self.ip+self.url["backup"]+"/"+self.ip)
if(r.status_code != requests.codes.ok):
raise Exception("Backup Request Failed")
newFile = open(output, "wb")
newFile.write(r.content)
newFile.close()
def restore(self, i):
raise Exception("the restore method is still untested.")
newFile = open(i, "rb")
data = ""
for a in newFile:
data += a
newFile.close()
r = self._post("restore", data)
print(r.json())
if(r.status_code != requests.codes.ok):
raise Exception("Restore Request Failed")
r = self._get("reboot")
return r.json()
def getMAC(self):
r = self._get("mac", timeout=60)
if(r.status_code != requests.codes.ok):
raise Exception("Action failed")
self.mac = r.json()["MACTable"]
def getID(self):
r = self._get("id", params={"_": time.time()})
if(r.status_code != requests.codes.ok):
raise Exception("Action failed")
self.id = r.json()["BootID"]
def getStatus(self):
if(self.id == ""):
self.getID()
r = self.s.get("https://"+self.ip+self.url["status"]+"?%s&_=%d" % (self.id, time.time()))
if(r.status_code != requests.codes.ok):
raise Exception("Action failed")
self.status = r.json()
def update(self, i):
data = ""
with open(i, mode='rb') as file: # b is important -> binary
data = file.read()
r = self._post("update", data)
if(r.status_code != requests.codes.ok):
raise Exception("Firmware Upload Failed")
r = self._get("doupdate")
if(r.status_code != requests.codes.ok):
raise Exception("Update Request Failed")
def mergeConfig(self, config):
self.orig_config = deepcopy(self.config)
for k, v in config.items():
if(k == "Ports"):
self._merge_by_key(self.config[k], v, key="Number")
continue
if(k == "LACP"):
self._merge_by_key(self.config[k], v, key="Port")
continue
if(k == "VLANs"):
self._merge_by_key(self.config[k], v, key="ID")
continue
if(type(v) is dict):
continue
if(type(v) is list):
self.config[k] += v
continue
self.config[k] = v
def replaceConfig(self, config):
self.orig_config = deepcopy(self.config)
if("Config_Version" in config):
del config["Config_Version"]
self.config.update(config)
def getDiff(self):
if(self.orig_config is None):
return {}
if(DIFF is False):
raise ImportError("Missing DeepDiff Module")
return DeepDiff(
self.orig_config,
self.config,
exclude_paths="root['Config_Version']"
)
if __name__ == '__main__':
import getpass
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
ip = str(input("switch ip:"))
user = str(input("user:"))
pw = getpass.getpass("password:")
n = Netonix()
n.open(ip, user, pw)
n.getMAC()
print(json.dumps(n.mac, indent=4))
n.getMAC()
print(json.dumps(n.mac, indent=4))