diff options
author | Ludovic Pouzenc <ludovic@pouzenc.fr> | 2016-05-15 13:05:33 +0200 |
---|---|---|
committer | Ludovic Pouzenc <ludovic@pouzenc.fr> | 2016-05-15 13:05:33 +0200 |
commit | 928aa3ef114ebf9246c90f9eae80da9a20172530 (patch) | |
tree | a30537ba248d023f863c0e61bdab8da73842d3ec /haircontrol/discovery.py | |
parent | 2e59a405596156412ff7240934071be76ec30301 (diff) | |
download | haircontrol-928aa3ef114ebf9246c90f9eae80da9a20172530.zip haircontrol-928aa3ef114ebf9246c90f9eae80da9a20172530.tar.gz haircontrol-928aa3ef114ebf9246c90f9eae80da9a20172530.tar.bz2 |
Add ip link support to mac local_macs, improove test result readability
Diffstat (limited to 'haircontrol/discovery.py')
-rw-r--r-- | haircontrol/discovery.py | 31 |
1 files changed, 21 insertions, 10 deletions
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<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 + IPNEIGH = re.compile("(?P<ip>[a-f0-9:.]+) dev (?P<ifname>.*) lladdr (?P<mac>[a-f0-9:]*)") + IPLINKSHOW = re.compile("(?P<id>\d+):\s+(?P<ifname>[^:]*):.+\s+(?:link/ether\s+(?P<mac>[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) |