-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwisp.py
173 lines (138 loc) · 5.29 KB
/
wisp.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pywisp_emibcn.aircontrol import ACSession
from pprint import pprint
class Wisp():
'''WISP interface to define infrastructure and host matching and authentication mechanisms'''
ac_conf = {
"url": "https://localhost:9082",
"user": "admin",
"password": "admin",
}
__ac = None
__log = None
__getlogger = None
def __init__(self, ac_conf=None, getlog=None):
if getlog:
self.getlogger = getlog
if ac_conf:
self.ac_conf = ac_conf
self.log.debug("__init__: Loaded AC configuration: %s" %
(self.ac_conf))
@property
def log(self):
if not self.__log:
self.__log = self.getlogger(__name__)
return self.__log
@log.setter
def log(self, log):
self.__log = log
@property
def getlogger(self):
if not self.__getlogger:
def noop(x, y): return x
return lambda name: type('fakelogger', (object,), {'error': print, 'warning': print, 'info': noop, 'debug': noop})()
return self.__getlogger
@getlogger.setter
def getlogger(self, getlogger):
self.__getlogger = getlogger
self.log = None
@property
def ac(self):
if not self.__ac:
self.__ac = ACSession(
self.ac_conf['url'], self.ac_conf['user'], self.ac_conf['password'])
self.__ac.login()
return self.__ac
@ac.setter
def ac(self, ac):
self.__ac = ac
def get_host(self, name, deep=False, from_br=None):
raise NotImplementedError("Should have implemented `get_host` method")
def get_ac_devices(self):
'''Generate antennas list (with credentials)'''
raise NotImplementedError(
"Should have implemented `get_ac_devices` method")
def get_ac_brs(self):
'''Generate antennas list (with credentials) only of BRs'''
raise NotImplementedError(
"Should have implemented `get_ac_brs` method")
def get_mt_devices(self):
'''Generate antennas list (with credentials)'''
raise NotImplementedError(
"Should have implemented `get_mt_devices` method")
def get_aircontrol_deep(self, name, from_br=None):
self.log.info("Download BRs...")
repetidors = self.get_ac_brs(from_br=None)
self.log.info("BRs found: %s" % (len(repetidors)))
clients_total = []
clients_wifi = []
for repetidor in repetidors:
try:
self.log.info("Download wifi stations from %s" % (repe.name))
clients_wifi = repetidor.getWifiStations()
except Exception as e:
self.log.warning(
"There was a problem connecting to %s: %s" % (repe.name, str(e)))
continue
clients = [
{
'deviceId': -1,
'properties': {
'hostname': cw['remote']['hostname'],
'ip': cw['lastip'],
'mac': cw['mac'],
}
}
for cw in clients_wifi
if not name or
(
name in cw['mac'].lower() or
name in cw['lastip'] or
('remote' in cw and name in cw['remote']
['hostname'].lower())
)
]
for client in clients:
self.log.info("%s - %s - %s" % (client['properties']['hostname'],
client['properties']['mac'], client['properties']['ip']))
clients_total += clients
return clients_total
def ac_reorder_branches(self):
# Get devices list
devices = self.get_ac_devices(self.ac.getDevices())
# Separate BRs and the rest of clients
brs = []
clients = []
for antena in antenas:
if antena.data['properties']['wlanOpModeString'] != 'sta':
brs.append(antena)
else:
clients.append(antena)
# For each BR
done = 0
patchList = []
for br in brs:
self.log.info("- %s (%s)" % (br.name, br.id))
# For each client connectetd to it's SSID
for client in clients:
if client.ssid == br.ssid:
if client.branch == br.id:
self.log.info(" - %s (%s)" %
(client.name, client.ssid))
else:
done += 1
self.log.info(" - Move {client} ({clid} - {clessid}) from {branch} to {brname} ({brid})".format(
client=client.name,
clid=client.id,
clessid=client.ssid,
branch=client.branch,
brname=br.name,
brid=br.id))
patchList.append(
self.ac.patchDeviceCreate(
client.id, {"parentId": br.id})
)
# Send patches
self.ac.patchDeviceList(patchList)
self.log.info("Done: %d" % (done + 1))