annotate service/tomatoWifi/wifi.py @ 1146:b62ee2697b8b

wifi support for scraping Orbi admin page Ignore-this: b49c8e63fb2e0eeb2757958b1c10dcae darcs-hash:cc321e9add92e4d48a23f576ad8343ec71c15455
author drewp <drewp@bigasterisk.com>
date Sat, 03 Mar 2018 18:14:08 -0800
parents d4218c0e0d30
children fdbdecdecd0b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
6
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
7 log = logging.getLogger()
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
13 class Wifi(object):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
14 """
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
15 gather the users of wifi from the tomato routers
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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"):
1077
d4218c0e0d30 reload config wifi more often
drewp <drewp@bigasterisk.com>
parents: 1007
diff changeset
18 self.rereadConfig()
d4218c0e0d30 reload config wifi more often
drewp <drewp@bigasterisk.com>
parents: 1007
diff changeset
19 #self._loadRouters(accessN3, tomatoUrl)
d4218c0e0d30 reload config wifi more often
drewp <drewp@bigasterisk.com>
parents: 1007
diff changeset
20
d4218c0e0d30 reload config wifi more often
drewp <drewp@bigasterisk.com>
parents: 1007
diff changeset
21 def rereadConfig(self):
980
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
22 self.graph = Graph()
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
23 self.graph.parse('config.n3', format='n3')
1077
d4218c0e0d30 reload config wifi more often
drewp <drewp@bigasterisk.com>
parents: 1007
diff changeset
24
980
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
25
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
26 def _loadRouters(self, accessN3, tomatoUrl):
805
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
27 g = Graph()
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
28 g.parse(accessN3, format="n3")
980
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
29 repl = {
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
30 '/wifiRouter1/' : None,
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
31 #'/tomato2/' : None
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
32 }
805
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
33 for k in repl:
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
34 rows = list(g.query('''
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
35 PREFIX p: <http://bigasterisk.com/openid_proxy#>
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
36 SELECT ?prefix WHERE {
980
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
37 ?site
805
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
38 p:requestPrefix ?public;
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
39 p:proxyUrlPrefix ?prefix
980
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
40 .
805
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
41 }''', initBindings={"public" : Literal(k)}))
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
42 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
43 log.debug('repl %r', repl)
805
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
44
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
45 self.routers = []
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
46 for url in tomatoUrl:
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
47 name = url
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
48 for k, v in repl.items():
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
49 url = url.replace(k, v)
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
50
856
4b386fc51325 rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents: 841
diff changeset
51 r = Router()
857
1168b0cd90af fix fetch calls in tomatowifi
drewp <drewp@bigasterisk.com>
parents: 856
diff changeset
52 http, tail = url.split('//', 1)
1168b0cd90af fix fetch calls in tomatowifi
drewp <drewp@bigasterisk.com>
parents: 856
diff changeset
53 userPass, tail = tail.split("@", 1)
1168b0cd90af fix fetch calls in tomatowifi
drewp <drewp@bigasterisk.com>
parents: 856
diff changeset
54 r.url = http + '//' + tail
1168b0cd90af fix fetch calls in tomatowifi
drewp <drewp@bigasterisk.com>
parents: 856
diff changeset
55 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
56 r.name = {'wifiRouter1' : 'bigasterisk5',
805
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
57 'tomato2' : 'bigasterisk4'}[name.split('/')[1]]
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
58 self.routers.append(r)
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
59
856
4b386fc51325 rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents: 841
diff changeset
60 @inlineCallbacks
805
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
61 def getPresentMacAddrs(self):
1077
d4218c0e0d30 reload config wifi more often
drewp <drewp@bigasterisk.com>
parents: 1007
diff changeset
62 self.rereadConfig()
1146
b62ee2697b8b wifi support for scraping Orbi admin page
drewp <drewp@bigasterisk.com>
parents: 1077
diff changeset
63 rows = yield loadOrbiData()
980
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
64 for row in rows:
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
65 if 'clientHostname' in row:
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
66 row['name'] = row['clientHostname']
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
67 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
68 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
69 if label:
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
70 row['name'] = label
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
71 returnValue(rows)
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
72
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
73 @inlineCallbacks
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
74 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
75 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
76
805
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
77 for router in self.routers:
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
78 log.debug("GET %s", router)
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
79 try:
857
1168b0cd90af fix fetch calls in tomatowifi
drewp <drewp@bigasterisk.com>
parents: 856
diff changeset
80 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
81 timeout=2)
805
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
82 except socket.error:
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
83 log.warn("get on %s failed" % router)
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
84 continue
857
1168b0cd90af fix fetch calls in tomatowifi
drewp <drewp@bigasterisk.com>
parents: 856
diff changeset
85 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
86 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
87 # zyxel 'Station Info' page
980
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
88 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
89 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
90 # tomato page
980
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
91 rows.extend(self._parseTomato(data, router.name))
856
4b386fc51325 rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents: 841
diff changeset
92
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
93 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
94 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
95 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
96 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
97 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
98
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 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
100
980
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
101 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
102 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
103 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
104 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
105 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
106 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
107 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
108 yield dict(router=routerName, mac=mac, assoc=assoc, connected=assoc)
805
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
109
980
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
110 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
111 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
112 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
113
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
114
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
115 @inlineCallbacks
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
116 def loadUvaData():
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
117 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
118 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
119 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
120 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
121 byMac = {}
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
122 for tr in root.cssselect('tr'):
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
123 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
124 if mac == "MAC":
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
125 continue
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
126 connected = connected.lower() == 'yes'
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
127 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
128
980
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
129 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
130 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
131 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
132 if clientHostname == 'wlanadv.none':
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
133 continue
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
134 byMac.setdefault(mac, {}).update(dict(
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
135 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
136
2f1cb8b5950a rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents: 867
diff changeset
137 returnValue(sorted(byMac.values()))
1007
6c30561d7d30 start of code for reading cisco router data
drewp <drewp@bigasterisk.com>
parents: 980
diff changeset
138
6c30561d7d30 start of code for reading cisco router data
drewp <drewp@bigasterisk.com>
parents: 980
diff changeset
139 @inlineCallbacks
6c30561d7d30 start of code for reading cisco router data
drewp <drewp@bigasterisk.com>
parents: 980
diff changeset
140 def loadCiscoData():
6c30561d7d30 start of code for reading cisco router data
drewp <drewp@bigasterisk.com>
parents: 980
diff changeset
141 config = json.load(open("/my/proj/homeauto/service/tomatoWifi/priv-uva.json"))
6c30561d7d30 start of code for reading cisco router data
drewp <drewp@bigasterisk.com>
parents: 980
diff changeset
142 headers = {'Authorization': ['Basic %s' % config['userPass'].encode('base64').strip()]}
6c30561d7d30 start of code for reading cisco router data
drewp <drewp@bigasterisk.com>
parents: 980
diff changeset
143 print headers
6c30561d7d30 start of code for reading cisco router data
drewp <drewp@bigasterisk.com>
parents: 980
diff changeset
144 resp = yield fetch('http://10.2.0.2/', headers=headers)
6c30561d7d30 start of code for reading cisco router data
drewp <drewp@bigasterisk.com>
parents: 980
diff changeset
145 print resp.body
6c30561d7d30 start of code for reading cisco router data
drewp <drewp@bigasterisk.com>
parents: 980
diff changeset
146 returnValue([])
1146
b62ee2697b8b wifi support for scraping Orbi admin page
drewp <drewp@bigasterisk.com>
parents: 1077
diff changeset
147
b62ee2697b8b wifi support for scraping Orbi admin page
drewp <drewp@bigasterisk.com>
parents: 1077
diff changeset
148 @inlineCallbacks
b62ee2697b8b wifi support for scraping Orbi admin page
drewp <drewp@bigasterisk.com>
parents: 1077
diff changeset
149 def loadOrbiData():
b62ee2697b8b wifi support for scraping Orbi admin page
drewp <drewp@bigasterisk.com>
parents: 1077
diff changeset
150 config = json.load(open("/my/proj/homeauto/service/tomatoWifi/priv-uva.json"))
b62ee2697b8b wifi support for scraping Orbi admin page
drewp <drewp@bigasterisk.com>
parents: 1077
diff changeset
151 headers = {'Authorization': ['Basic %s' % config['userPass'].encode('base64').strip()]}
b62ee2697b8b wifi support for scraping Orbi admin page
drewp <drewp@bigasterisk.com>
parents: 1077
diff changeset
152 resp = yield fetch('http://orbi.bigasterisk.com/DEV_device_info.htm', headers=headers)
b62ee2697b8b wifi support for scraping Orbi admin page
drewp <drewp@bigasterisk.com>
parents: 1077
diff changeset
153
b62ee2697b8b wifi support for scraping Orbi admin page
drewp <drewp@bigasterisk.com>
parents: 1077
diff changeset
154 if not resp.body.startswith(('device=', 'device_changed=0\ndevice=', 'device_changed=1\ndevice=')):
b62ee2697b8b wifi support for scraping Orbi admin page
drewp <drewp@bigasterisk.com>
parents: 1077
diff changeset
155 raise ValueError(resp.body)
b62ee2697b8b wifi support for scraping Orbi admin page
drewp <drewp@bigasterisk.com>
parents: 1077
diff changeset
156
b62ee2697b8b wifi support for scraping Orbi admin page
drewp <drewp@bigasterisk.com>
parents: 1077
diff changeset
157 ret = []
b62ee2697b8b wifi support for scraping Orbi admin page
drewp <drewp@bigasterisk.com>
parents: 1077
diff changeset
158 for row in json.loads(resp.body.split('device=', 1)[-1]):
b62ee2697b8b wifi support for scraping Orbi admin page
drewp <drewp@bigasterisk.com>
parents: 1077
diff changeset
159 ret.append(dict(
b62ee2697b8b wifi support for scraping Orbi admin page
drewp <drewp@bigasterisk.com>
parents: 1077
diff changeset
160 connected=True,
b62ee2697b8b wifi support for scraping Orbi admin page
drewp <drewp@bigasterisk.com>
parents: 1077
diff changeset
161 ipaddr=row['ip'],
b62ee2697b8b wifi support for scraping Orbi admin page
drewp <drewp@bigasterisk.com>
parents: 1077
diff changeset
162 mac=row['mac'].lower(),
b62ee2697b8b wifi support for scraping Orbi admin page
drewp <drewp@bigasterisk.com>
parents: 1077
diff changeset
163 contype=row['contype'],
b62ee2697b8b wifi support for scraping Orbi admin page
drewp <drewp@bigasterisk.com>
parents: 1077
diff changeset
164 model=row['model'],
b62ee2697b8b wifi support for scraping Orbi admin page
drewp <drewp@bigasterisk.com>
parents: 1077
diff changeset
165 clientHostname=row['name'] if row['name'] != 'Unknown' else None))
b62ee2697b8b wifi support for scraping Orbi admin page
drewp <drewp@bigasterisk.com>
parents: 1077
diff changeset
166 returnValue(ret)
b62ee2697b8b wifi support for scraping Orbi admin page
drewp <drewp@bigasterisk.com>
parents: 1077
diff changeset
167
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
168
805
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
169 def jsValue(js, variableName):
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
170 # using literal_eval instead of json parser to handle the trailing commas
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
171 val = re.search(variableName + r'\s*=\s*(.*?);', js, re.DOTALL).group(1)
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
172 return ast.literal_eval(val)