From 928aa3ef114ebf9246c90f9eae80da9a20172530 Mon Sep 17 00:00:00 2001 From: Ludovic Pouzenc Date: Sun, 15 May 2016 13:05:33 +0200 Subject: Add ip link support to mac local_macs, improove test result readability --- haircontrol/discovery.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'haircontrol/discovery.py') diff --git a/haircontrol/discovery.py b/haircontrol/discovery.py index 2b7ad83..680eb4c 100644 --- a/haircontrol/discovery.py +++ b/haircontrol/discovery.py @@ -4,9 +4,8 @@ import xml.etree.ElementTree from haircontrol.data import * class Discovery: - IPNEIGH = re.compile("^(?P[a-f0-9:.]+) dev (?P.*) lladdr (?P[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 + IPNEIGH = re.compile("(?P[a-f0-9:.]+) dev (?P.*) lladdr (?P[a-f0-9:]*)") + IPLINKSHOW = re.compile("(?P\d+):\s+(?P[^:]*):.+\s+(?:link/ether\s+(?P[a-f0-9:]*)\s+brd|link/none)") def __init__(self, inspector): self.inspector = inspector @@ -15,18 +14,30 @@ class Discovery: def discover_hinting_from_lldp(self, e_lldp): self.net.add_equipment(e_lldp) self.inspector.connect(e_lldp) + + # Learn local interfaces of e_lldp + fd = self.inspector.command('ip-link') + for line in fd: + matches = Discovery.IPLINKSHOW.match(line) + if matches: + ifname, mac = [ matches.group(k) for k in ['ifname','mac'] ] + e_lldp.ifaces[ifname] = Interface(ifname, mac) + fd.close() + + # Create equipments and ifaces from LLDP neighbour discovery fd = self.inspector.command('lldp') root = xml.etree.ElementTree.parse(fd).getroot() for iface in root.iter('interface'): - ifname = iface.get('name') + local_ifname = iface.get('name') + local_mac = e_lldp.ifaces[local_ifname].mac chassis = iface.find('chassis') - e = Equipment() - e.name = chassis.find('name').text - e.mgmtip = chassis.find('mgmt-ip').text + remote_name = chassis.find('name').text + remote_ipmgmt = chassis.find('mgmt-ip').text + e = Equipment(remote_name, remote_ipmgmt) self.net.add_equipment(e) for port in iface.findall('port'): - #remote_ifname = port.find('id').text - e.add_seen_mac(ifname, 'lldp') #XXX + remote_ifname = port.find('id').text + e.add_seen_mac(remote_ifname, local_mac) fd.close() self.inspector.disconnect() @@ -35,7 +46,7 @@ class Discovery: self.inspector.connect(e_root) fd = self.inspector.command('ip-neigh') for line in fd: - matches = Discovery.IPNEIGH.search(line) + matches = Discovery.IPNEIGH.match(line) if matches: ip, ifname, mac = [ matches.group(k) for k in ['ip','ifname','mac'] ] self.net.index_mac_ip(mac, ip) -- cgit v1.1