forked from calmh/unifi-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unifi-low-snr-reconnect
executable file
·58 lines (48 loc) · 2.41 KB
/
unifi-low-snr-reconnect
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
#!/usr/bin/env python
from __future__ import print_function
import argparse
import time
from collections import defaultdict
from unifi.controller import Controller
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--controller', default='unifi', help='the controller address (default "unifi")')
parser.add_argument('-u', '--username', default='admin', help='the controller username (default "admin")')
parser.add_argument('-p', '--password', default='', help='the controller password')
parser.add_argument('-b', '--port', default='8443', help='the controller port (default "8443")')
parser.add_argument('-v', '--version', default='v2', help='the controller base version (default "v2")')
parser.add_argument('-s', '--siteid', default='default', help='the site ID, UniFi >=3.x only (default "default")')
parser.add_argument('-m', '--minsnr', default=15, type=int, help='(dB) minimum required client SNR (default 15)')
parser.add_argument('-i', '--checkintv', default=5, type=int, help='(s) check interval (default 5)')
parser.add_argument('-o', '--holdintv', default=300, type=int, help='(s) holddown interval (default 300)')
parser.add_argument('-d', '--debug', help='enable debug output', action='store_true')
args = parser.parse_args()
c = Controller(args.controller, args.username, args.password, args.port, args.version, args.siteid)
all_aps = c.get_aps()
ap_names = dict([(ap['mac'], ap.get('name')) for ap in all_aps])
kicks = defaultdict(int)
held = defaultdict(bool)
while True:
res = c.get_clients()
now = time.time()
if args.debug:
print('Got %d clients to check' % len(res))
for sta in res:
name = sta.get('hostname','')
snr = sta['rssi']
mac = sta['mac']
ap_mac = sta['ap_mac']
ap_name = ap_names[ap_mac]
essid = sta['essid']
if args.debug:
print('%s/%s@%s %d' % (name, mac, ap_name, snr))
if snr < args.minsnr:
if now - kicks[name] > args.holdintv:
print('Disconnecting %s/%s@%s (SNR %d dB < %d dB) on %s' % (name, mac, ap_name, snr, args.minsnr,essid))
c.disconnect_client(mac)
kicks[name] = now
held[name] = False
elif not held[name]:
held[name] = True
if args.debug:
print('Ignoring %s/%s@%s (SNR %d dB < %d dB) (holddown)' % (name, mac, ap_name, snr, args.minsnr))
time.sleep(args.checkintv)