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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
import re
import json
import xml.etree.ElementTree
class Inspector():
cmds = {}
def __init__(self):
#XXX in a mockup class ?
self.testDataPath = '../test-data/input'
self.e = None
def connect(self, e):
self.e = e
self.e.inspected = 'in-progress'
def disconnect(self):
self.e.inspected = True
self.e = None
def command(self, cmdname):
cmddef = self.cmds.get(cmdname)
if not cmddef:
return None
cmd = cmddef['cmd']
result = []
fd = None
try:
fd = self._exec(cmdname, cmd)
re = cmddef.get('re')
func = cmddef.get('func')
if re:
for line in fd:
matches = re.match(line)
if matches:
result.append(matches.groups())
elif func:
result = func(self, fd)
except Exception:
print("Error : can't exec/read %s (%s) on %s (%s)"%(cmdname, cmd, self.e.name, self.e.mgmtip))
finally:
if fd:
fd.close()
return result
def _exec(self, cmdname, cmd):
#XXX in a mockup class ?
mockfile = self.testDataPath + '/' + self.e.name + '-' + cmdname + '.out'
return open(mockfile)
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 for %s (%s)"%(self.e.name, self.e.mgmtip))
finally:
fd.close()
return js
class DummyInspector(Inspector):
pass
class LinuxInspector(Inspector):
def parse_lldpctl_xml(self, fd):
result = []
root = xml.etree.ElementTree.parse(fd).getroot()
for iface in root.iter('interface'):
local_ifname = iface.get('name')
local_mac = self.e.ifaces[local_ifname].mac
chassis = iface.find('chassis')
remote_name = chassis.find('name').text
remote_ipmgmt = chassis.find('mgmt-ip').text
ports = []
for port in iface.findall('port'):
remote_ifname = port.find('id').text
ports.append(remote_ifname)
result.append( (local_ifname, local_mac, remote_name, remote_ipmgmt, ports) )
return result
cmds = {
'ip-neigh': {
'cmd': 'ip neigh',
're': re.compile("(?P<ip>[a-f0-9:.]+) dev (?P<ifname>.*) lladdr (?P<mac>[a-f0-9:]*)")
# fe80::8300 dev eth1 lladdr 10:fe:ed:f1:e1:f3 router STALE
# 172.16.20.210 dev eth1 lladdr c0:4a:00:fe:1f:87 REACHABLE
},
'ip-link': {
'cmd': 'ip -o link',
'kind': 'text',
're': re.compile("\d+:\s+(?P<ifname>[^:]*):.+\s+(?:link/ether\s+(?P<mac>[a-f0-9:]*)\s+brd|link/none)")
# 1: lo: <LOOPBACK,UP,LOWER_UP> 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: <BROADCAST,MULTICAST,UP,LOWER_UP> 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
},
'lldpctl': {
'cmd': 'lldpctl -f xml',
'func': parse_lldpctl_xml
#<?xml version="1.0" encoding="UTF-8"?>
#<lldp label="LLDP neighbors">
# <interface label="Interface" name="eth0" via="CDPv1" rid="2" age="0 day, 21:41:18">
# <chassis label="Chassis">
# <id label="ChassisID" type="local">Robert_VILO</id>
# <name label="SysName">Robert_VILO</name>
# <descr label="SysDescr">LM5 running on
#XM.v5.5.10</descr>
# <mgmt-ip label="MgmtIP">172.16.10.26</mgmt-ip>
# <mgmt-ip label="MgmtIP">169.254.227.212</mgmt-ip>
# <capability label="Capability" type="Bridge" enabled="on"/>
# </chassis>
# <port label="Port">
# <id label="PortID" type="ifname">br0</id>
# <descr label="PortDescr">br0</descr>
# </port>
# </interface>
# <interface label="Interface" name="eth0" via="CDPv1" rid="4" age="0 day, 21:41:18">
# ...
# </interface>
#</lldp>
},
}
class UbntInspector(Inspector):
def parse_status_json(self, fd):
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):
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':'/usr/www/status.cgi',
'func': parse_status_json
},
'brmacs.cgi': {
'cmd':'QUERY_STRING=brmacs=y /usr/www/brmacs.cgi',
'func': parse_brmacs_json
}
}
class ToughSwitchInspector(Inspector):
def parse_mactable_data_cgi(self, fd):
result = []
macs = self._parse_cgi_json(fd).get('macs')
if macs:
for line in macs:
if isinstance(line, dict):
ifname = "p%i"%line.get('port')
mac = line.get('mac')
result.append( (ifname, mac) )
return result
cmds = {
'mactable_data.cgi': {
'cmd':'/usr/www/mactable_data.cgi',
'func': parse_mactable_data_cgi
},
}
class EdgeMaxInspector(Inspector):
cmds = {
'mac-addr-table': {
'cmd':'show mac-addr-table', #XXX needs "enable" mode
're': re.compile("\d+\s+(?P<mac>[A-F0-9:]+)\s+(?P<ifname>[0-9/]+)\s")
# VLAN ID MAC Address Interface IfIndex Status
# 1 00:15:6D:8E:22:46 0/15 15 Learned
# 1 00:27:22:0E:67:F9 0/17 17 Learned
},
}
#TODO Implement them
class NetonixInspector(Inspector):
pass
class MikrotikInspector(Inspector):
pass
class OpenWRTInspector(Inspector):
pass
|