Mercurial > code > home > repos > homeauto
annotate service/tomatoWifi/wifi.py @ 1000:df5a40967a2f
polymer updates
Ignore-this: f4f88b324b54abf90af6dc5360910041
darcs-hash:20150830185325-312f9-8f07ba7ccc6ecd6528159fa5fd09a166d3ce1650
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Sun, 30 Aug 2015 11:53:25 -0700 |
parents | 2f1cb8b5950a |
children | 7faf642438bc |
rev | line source |
---|---|
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
1 import re, ast, logging, socket, json |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
2 import lxml.html.soupparser |
856
4b386fc51325
rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents:
841
diff
changeset
|
3 from twisted.internet.defer import inlineCallbacks, returnValue |
4b386fc51325
rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents:
841
diff
changeset
|
4 from cyclone.httpclient import fetch |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
5 from rdflib import Literal, Graph, RDFS, URIRef |
805 | 6 |
7 log = logging.getLogger() | |
8 | |
856
4b386fc51325
rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents:
841
diff
changeset
|
9 class Router(object): |
4b386fc51325
rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents:
841
diff
changeset
|
10 def __repr__(self): |
4b386fc51325
rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents:
841
diff
changeset
|
11 return repr(self.__dict__) |
4b386fc51325
rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents:
841
diff
changeset
|
12 |
805 | 13 class Wifi(object): |
14 """ | |
15 gather the users of wifi from the tomato routers | |
16 """ | |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
17 def __init__(self, accessN3="/my/proj/openid_proxy/access.n3"): |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
18 self.graph = Graph() |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
19 self.graph.parse('config.n3', format='n3') |
805 | 20 |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
21 #self._loadRouters(accessN3, tomatoUrl) |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
22 |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
23 def _loadRouters(self, accessN3, tomatoUrl): |
805 | 24 g = Graph() |
25 g.parse(accessN3, format="n3") | |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
26 repl = { |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
27 '/wifiRouter1/' : None, |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
28 #'/tomato2/' : None |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
29 } |
805 | 30 for k in repl: |
31 rows = list(g.query(''' | |
32 PREFIX p: <http://bigasterisk.com/openid_proxy#> | |
33 SELECT ?prefix WHERE { | |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
34 ?site |
805 | 35 p:requestPrefix ?public; |
36 p:proxyUrlPrefix ?prefix | |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
37 . |
805 | 38 }''', initBindings={"public" : Literal(k)})) |
39 repl[k] = str(rows[0][0]) | |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
40 log.debug('repl %r', repl) |
805 | 41 |
42 self.routers = [] | |
43 for url in tomatoUrl: | |
44 name = url | |
45 for k, v in repl.items(): | |
46 url = url.replace(k, v) | |
47 | |
856
4b386fc51325
rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents:
841
diff
changeset
|
48 r = Router() |
857
1168b0cd90af
fix fetch calls in tomatowifi
drewp <drewp@bigasterisk.com>
parents:
856
diff
changeset
|
49 http, tail = url.split('//', 1) |
1168b0cd90af
fix fetch calls in tomatowifi
drewp <drewp@bigasterisk.com>
parents:
856
diff
changeset
|
50 userPass, tail = tail.split("@", 1) |
1168b0cd90af
fix fetch calls in tomatowifi
drewp <drewp@bigasterisk.com>
parents:
856
diff
changeset
|
51 r.url = http + '//' + tail |
1168b0cd90af
fix fetch calls in tomatowifi
drewp <drewp@bigasterisk.com>
parents:
856
diff
changeset
|
52 r.headers = {'Authorization': ['Basic %s' % userPass.encode('base64').strip()]} |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
53 r.name = {'wifiRouter1' : 'bigasterisk5', |
805 | 54 'tomato2' : 'bigasterisk4'}[name.split('/')[1]] |
55 self.routers.append(r) | |
56 | |
856
4b386fc51325
rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents:
841
diff
changeset
|
57 @inlineCallbacks |
805 | 58 def getPresentMacAddrs(self): |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
59 rows = yield loadUvaData() |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
60 for row in rows: |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
61 if 'clientHostname' in row: |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
62 row['name'] = row['clientHostname'] |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
63 mac = URIRef('http://bigasterisk.com/mac/%s' % row['mac'].lower()) |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
64 label = self.graph.value(mac, RDFS.label) |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
65 if label: |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
66 row['name'] = label |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
67 returnValue(rows) |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
68 |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
69 @inlineCallbacks |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
70 def getPresentMacAddrs_multirouter(self): |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
71 rows = [] |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
72 |
805 | 73 for router in self.routers: |
74 log.debug("GET %s", router) | |
75 try: | |
857
1168b0cd90af
fix fetch calls in tomatowifi
drewp <drewp@bigasterisk.com>
parents:
856
diff
changeset
|
76 resp = yield fetch(router.url, headers=router.headers, |
856
4b386fc51325
rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents:
841
diff
changeset
|
77 timeout=2) |
805 | 78 except socket.error: |
79 log.warn("get on %s failed" % router) | |
80 continue | |
857
1168b0cd90af
fix fetch calls in tomatowifi
drewp <drewp@bigasterisk.com>
parents:
856
diff
changeset
|
81 data = resp.body |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
82 if 'Wireless -- Authenticated Stations' in data: |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
83 # zyxel 'Station Info' page |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
84 rows.extend(self._parseZyxel(data, router.name)) |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
85 else: |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
86 # tomato page |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
87 rows.extend(self._parseTomato(data, router.name)) |
856
4b386fc51325
rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents:
841
diff
changeset
|
88 |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
89 for r in rows: |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
90 try: |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
91 r['name'] = self.knownMacAddr[r['mac']] |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
92 except KeyError: |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
93 pass |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
94 |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
95 returnValue(rows) |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
96 |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
97 def _parseZyxel(self, data, routerName): |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
98 root = lxml.html.soupparser.fromstring(data) |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
99 for tr in root.cssselect('tr'): |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
100 mac, assoc, uth, ssid, iface = [td.text_content().strip() for td in tr.getchildren()] |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
101 if mac == "MAC": |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
102 continue |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
103 assoc = assoc.lower() == 'yes' |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
104 yield dict(router=routerName, mac=mac, assoc=assoc, connected=assoc) |
805 | 105 |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
106 def _parseTomato(self, data, routerName): |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
107 for iface, mac, signal in jsValue(data, 'wldev'): |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
108 yield dict(router=routerName, mac=mac, signal=signal, connected=bool(signal)) |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
109 |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
110 |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
111 @inlineCallbacks |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
112 def loadUvaData(): |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
113 config = json.load(open("/my/proj/homeauto/service/tomatoWifi/priv-uva.json")) |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
114 headers = {'Authorization': ['Basic %s' % config['userPass'].encode('base64').strip()]} |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
115 resp = yield fetch('http://10.2.0.2/wlstationlist.cmd', headers=headers) |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
116 root = lxml.html.soupparser.fromstring(resp.body) |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
117 byMac = {} |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
118 for tr in root.cssselect('tr'): |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
119 mac, connected, auth, ssid, iface = [td.text_content().strip() for td in tr.getchildren()] |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
120 if mac == "MAC": |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
121 continue |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
122 connected = connected.lower() == 'yes' |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
123 byMac[mac] = dict(mac=mac, connected=connected, auth=auth == 'Yes', ssid=ssid, iface=iface) |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
124 |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
125 resp = yield fetch('http://10.2.0.2/DHCPTable.asp', headers=headers) |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
126 for row in re.findall(r'new AAA\((.*)\)', resp.body): |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
127 clientHostname, ipaddr, mac, expires, iface = [s.strip("'") for s in row.rsplit(',', 4)] |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
128 if clientHostname == 'wlanadv.none': |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
129 continue |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
130 byMac.setdefault(mac, {}).update(dict( |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
131 clientHostname=clientHostname, connection=iface, ipaddr=ipaddr, dhcpExpires=expires)) |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
132 |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
133 returnValue(sorted(byMac.values())) |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
134 |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
135 |
805 | 136 def jsValue(js, variableName): |
137 # using literal_eval instead of json parser to handle the trailing commas | |
138 val = re.search(variableName + r'\s*=\s*(.*?);', js, re.DOTALL).group(1) | |
139 return ast.literal_eval(val) |