annotate service/tomatoWifi/wifi.py @ 202:7faf642438bc

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