LIBCLOUD-820 added arp cache check using iproute2

In case arp isn't accessable by the user (as is the case for me
on debian testing) the use ip neighbor

Closes #788

Signed-off-by: Tomaz Muraus <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/bff3f293
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/bff3f293
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/bff3f293

Branch: refs/heads/trunk
Commit: bff3f29321c38825f4be881bc344487aa3052369
Parents: 9975bca
Author: René Kjellerup <[email protected]>
Authored: Fri May 27 17:09:05 2016 -0700
Committer: Tomaz Muraus <[email protected]>
Committed: Sat May 28 19:26:11 2016 +0200

----------------------------------------------------------------------
 libcloud/compute/drivers/libvirt_driver.py | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/bff3f293/libcloud/compute/drivers/libvirt_driver.py
----------------------------------------------------------------------
diff --git a/libcloud/compute/drivers/libvirt_driver.py 
b/libcloud/compute/drivers/libvirt_driver.py
index 34389d7..d586d82 100644
--- a/libcloud/compute/drivers/libvirt_driver.py
+++ b/libcloud/compute/drivers/libvirt_driver.py
@@ -263,11 +263,20 @@ class LibvirtNodeDriver(NodeDriver):
 
         mac_addresses = self._get_mac_addresses_for_domain(domain=domain)
 
-        cmd = ['arp', '-an']
-        child = subprocess.Popen(cmd, stdout=subprocess.PIPE,
+        arp_table = {}
+        try:
+            cmd = ['arp', '-an']
+            child = subprocess.Popen(cmd, stdout=subprocess.PIPE,
+                                 stderr=subprocess.PIPE)
+            stdout, _ = child.communicate()
+            arp_table = self._parse_arp_table(arp_output=stdout)
+        except OSError as e:
+            if e.errno == 2:
+                cmd = ['ip', 'neigh']
+                child = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE)
-        stdout, _ = child.communicate()
-        arp_table = self._parse_arp_table(arp_output=stdout)
+                stdout, _ = child.communicate()
+                arp_table = 
self._parse_arp_table(arp_output=stdout,re_match='(.*?)\s+.*lladdr\s+(.*?)\s+')
 
         for mac_address in mac_addresses:
             if mac_address in arp_table:
@@ -314,7 +323,7 @@ class LibvirtNodeDriver(NodeDriver):
 
         return result
 
-    def _parse_arp_table(self, arp_output):
+    def _parse_arp_table(self, arp_output, re_match='.*?\((.*?)\) at 
(.*?)\s+'):
         """
         Parse arp command output and return a dictionary which maps mac address
         to an IP address.
@@ -322,11 +331,12 @@ class LibvirtNodeDriver(NodeDriver):
         :return: Dictionary which maps mac address to IP address.
         :rtype: ``dict``
         """
+        ip_mac = re.compile(re_match)
         lines = arp_output.split('\n')
 
         arp_table = defaultdict(list)
         for line in lines:
-            match = re.match('.*?\((.*?)\) at (.*?)\s+', line)
+            match = ip_mac.match(line)
 
             if not match:
                 continue

Reply via email to