correct parsing of interface with whitespace

This commit is contained in:
Petr Klima committed 2020-11-06 13:16:55 +01:00
1 parent 8c7c4e5a9b
commit ed117140cd
3 files changed
+52 -11

No files matched your search

+47 -9
View File
@@ -387,24 +387,42 @@ class S350Driver(NetworkDriver):
def get_interfaces_ip(self): def get_interfaces_ip(self):
"""Returns all configured interface IP addresses.""" """Returns all configured interface IP addresses."""
interfaces = {} interfaces = {}
valid_interfaces = []
show_ip_int = self._send_command('show ip int') show_ip_int = self._send_command('show ip int')
# Limit to valid interfaces (i.e. ignore vlan 1) header = True # cycle trought header
for line in show_ip_int.splitlines(): for line in show_ip_int.splitlines():
if ('UP' in line or 'DOWN' in line) and line.split()[-1] == 'Valid': if header:
valid_interfaces.append(line) # last line of first header
match = re.match(r'^---+ -+ .*$', line)
if match:
header = False
fields_end = self._get_ip_int_fields_end(line)
continue
for interface in valid_interfaces: # next header, stop processing text
network, name = interface.split()[:2] if re.match(r'^---+ -+ .*$', line):
break
ip = netaddr.IPNetwork(network) line_elems = self._get_ip_int_line_to_fields(line, fields_end)
# only valid interfaces
# v 2.x firmware
if 7 in line_elems.keys():
if line_elems[7] != 'Valid':
continue
# v 1.x firmware
elif line_elems[5] != 'Valid':
continue
cidr = line_elems[0]
interface = line_elems[1]
ip = netaddr.IPNetwork(cidr)
family = 'ipv{0}'.format(ip.version) family = 'ipv{0}'.format(ip.version)
interfaces[name] = { interfaces[interface] = {
family: { family: {
str(ip.ip): { str(ip.ip): {
'prefix_length': ip.prefixlen 'prefix_length': ip.prefixlen
} }
} }
@@ -412,6 +430,26 @@ class S350Driver(NetworkDriver):
return interfaces return interfaces
def _get_ip_int_line_to_fields(self, line, fields_end):
""" dynamic fields lenghts """
line_elems = {}
index = 0
f_start = 0
for f_end in fields_end:
line_elems[index] = line[f_start:f_end].strip()
index += 1
f_start = f_end
return line_elems
def _get_ip_int_fields_end(self, dashline):
""" fields length are diferent device to device, detect them on horizontal lin """
fields_end = [m.start() for m in re.finditer(' ', dashline)]
# fields_position.insert(0,0)
fields_end.append(len(dashline))
return fields_end
def get_lldp_neighbors(self): def get_lldp_neighbors(self):
"""get_lldp_neighbors implementation for s350""" """get_lldp_neighbors implementation for s350"""
neighbors = {} neighbors = {}
@@ -1 +1 @@
{"vlan": {"ipv4": {"192.168.1.254": {"prefix_length": 24}}}} { "vlan 1" : { "ipv4" : { "192.168.1.254" : { "prefix_length" : 24 } } } }
@@ -1 +1,4 @@
{"vlan": {"ipv4": {"198.51.100.2": {"prefix_length": 24}}}} {
"vlan 1" : { "ipv4" : { "198.51.100.2" : { "prefix_length" : 24 } } },
"vlan 852" : { "ipv4" : { "198.51.100.1" : { "prefix_length" : 16 } } }
}