-
Notifications
You must be signed in to change notification settings - Fork 6
/
airconn.py
48 lines (44 loc) · 1.69 KB
/
airconn.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
#!/usr/bin/python2
import sys
from socket import *
from requests import post
headers = {'Authorization': "Bearer xxxxxxxxxx", 'content-type': 'application/json'}
host = '0.0.0.0'
port = 514
addr = (host,port)
try:
s = socket(AF_INET,SOCK_DGRAM)
s.bind(addr)
except:
print "error binding"
sys.exit(1)
try:
while 1:
try:
rdata,raddr = s.recvfrom(8092)
rmes = str(rdata)
#print 'got message from ',raddr,': ',rmes
if 'pppoe: Disconnected.' in rmes or 'ether: (WAN) link state is Down' in rmes:
postData = '{"state":"off","attributes": {"wan_ip":""}}'
#print postData
post('http://127.0.0.1:8123/api/states/binary_sensor.Internet',data=postData,headers=headers)
elif 'pppoe: Connection established' in rmes:
wan_ip = rmes[rmes.find('established')+12:rmes.find('->')-1]
postData = '{"state":"on","attributes": {"wan_ip":"'+wan_ip+'"}}'
#print postData
post('http://127.0.0.1:8123/api/states/binary_sensor.Internet',data=postData,headers=headers)
elif 'Disassociated with station' in rmes:
mac = rmes[-18:-1]
postData = '{"mac":"'+mac+'","source_type":"router","consider_home":"3"}'
#print postData
post('http://127.0.0.1:8123/api/services/device_tracker/see',data=postData,headers=headers)
elif 'Installed unicast CCMP key for supplicant' in rmes:
mac = rmes[-18:-1]
postData = '{"mac":"'+mac+'","source_type":"router","consider_home":"99:00:00"}'
#print postData
post('http://127.0.0.1:8123/api/services/device_tracker/see',data=postData,headers=headers)
except socket.error:
pass
except KeyboardInterrupt:
print('good bye')
sys.exit()