[virt-tools-list] [virt-manager PATCH 1/2] domain: Add function interface_addresses to return ipv4 and ipv6

Lin Ma lma at suse.com
Fri Aug 31 10:37:29 UTC 2018


It returns a dict that consisting of ipv4 and ipv6 which only match the
given mac, it returns None in exception.

Signed-off-by: Lin Ma <lma at suse.com>
---
 virtManager/domain.py | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/virtManager/domain.py b/virtManager/domain.py
index 5af6ee01..77de7ae7 100644
--- a/virtManager/domain.py
+++ b/virtManager/domain.py
@@ -1130,6 +1130,33 @@ class vmmDomain(vmmLibvirtObject):
 
         return self._backend.openGraphicsFD(0, flags)
 
+    def interface_addresses(self, source, mac):
+        def extract(info, mac):
+            import re
+            addrs = None
+            ipv4 = None
+            ipv6 = None
+            for iface in info:
+                if iface != "lo" and info[iface]["hwaddr"] == mac:
+                    addrs = info[iface]["addrs"]
+                    break
+            # In case of both of ipv4 and ipv6 not found
+            if addrs is None:
+                return {'ipv4': ipv4, 'ipv6': ipv6}
+            for addr in addrs:
+                if addr["type"] == 0:
+                    ipv4 = addr["addr"] + "/" + str(addr["prefix"])
+                elif addr["type"] == 1 and not re.match("^fe80", addr["addr"]):
+                    ipv6 = addr["addr"] + "/" + str(addr["prefix"])
+            return {'ipv4': ipv4, 'ipv6': ipv6}
+
+        try:
+            all_addrinfo = self._backend.interfaceAddresses(source)
+            addrinfo = extract(all_addrinfo, mac)
+        except Exception:
+            addrinfo = None
+        return addrinfo
+
     def refresh_snapshots(self):
         self._snapshot_list = None
 
-- 
2.15.1




More information about the virt-tools-list mailing list