From cc70191abcb928d2ec057b7aa04683c2ce8491e1 Mon Sep 17 00:00:00 2001 From: Ludovic Pouzenc Date: Mon, 16 May 2016 01:19:53 +0200 Subject: Implement parsing of brmacs.cgi and status.cgi (only for local interfaces) --- haircontrol/inspectors.py | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) (limited to 'haircontrol/inspectors.py') diff --git a/haircontrol/inspectors.py b/haircontrol/inspectors.py index 33bf360..f59287a 100644 --- a/haircontrol/inspectors.py +++ b/haircontrol/inspectors.py @@ -1,4 +1,5 @@ import re +import json import xml.etree.ElementTree class Inspector(): @@ -68,7 +69,7 @@ class LinuxInspector(Inspector): 'ip-link': { 'cmd': 'ip -o link', 'kind': 'text', - 're': re.compile("(?P\d+):\s+(?P[^:]*):.+\s+(?:link/ether\s+(?P[a-f0-9:]*)\s+brd|link/none)") + 're': re.compile("\d+:\s+(?P[^:]*):.+\s+(?:link/ether\s+(?P[a-f0-9:]*)\s+brd|link/none)") # 1: lo: mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default \ link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 # 2: eth0: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000\ link/ether 8c:89:a5:c7:be:88 brd ff:ff:ff:ff:ff:ff }, @@ -103,21 +104,50 @@ class LinuxInspector(Inspector): class UbntInspector(Inspector): + def _parse_cgi_json(self, fd): + for cgi_headers in fd: + if cgi_headers == '\n': + break + js = {} + try: + js = json.load(fd) + except ValueError: + print("Warn : unparsable json") + fd.close() + return js + def parse_status_json(self, fd): - return 'parse_status_json' + result = [] + interfaces = self._parse_cgi_json(fd).get('interfaces') + # XXX dup parse_brmacs_json(fd) and many other cool info to get + if interfaces: + for line in interfaces: + if isinstance(line, dict): + ifname = line.get('ifname') + mac = line.get('hwaddr') + result.append( (ifname, mac) ) + return result def parse_brmacs_json(self, fd): - return 'parse_brmacs_json' + result = [] + brmacs = self._parse_cgi_json(fd).get('brmacs') + if brmacs: + for line in brmacs: + if isinstance(line, dict): + ifname = line.get('port') + mac = line.get('hwaddr') + result.append( (ifname, mac) ) + return result + cmds = { 'status.cgi': { 'cmd':'..', - 'kind': 'cgi-json', 'func': parse_status_json }, 'brmacs.cgi': { 'cmd':'..', - 'kind': 'cgi-json', 'func': parse_brmacs_json } } + -- cgit v1.1