[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH v2 5/5] python: create example for dumping domain IP addresses



---
 examples/python/Makefile.am   |    2 +-
 examples/python/README        |    1 +
 examples/python/domipaddrs.py |   62 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 64 insertions(+), 1 deletions(-)
 create mode 100644 examples/python/domipaddrs.py

diff --git a/examples/python/Makefile.am b/examples/python/Makefile.am
index b04d126..b51f729 100644
--- a/examples/python/Makefile.am
+++ b/examples/python/Makefile.am
@@ -4,4 +4,4 @@
 EXTRA_DIST=						\
 	README						\
 	consolecallback.py				\
-	dominfo.py domrestore.py domsave.py domstart.py esxlist.py
+	dominfo.py domrestore.py domsave.py domstart.py esxlist.py domipaddrs.py
diff --git a/examples/python/README b/examples/python/README
index f4db76c..d895740 100644
--- a/examples/python/README
+++ b/examples/python/README
@@ -10,6 +10,7 @@ domsave.py  - save all running domU's into a directory
 domrestore.py - restore domU's from their saved files in a directory
 esxlist.py  - list active domains of an VMware ESX host and print some info.
               also demonstrates how to use the libvirt.openAuth() method
+domipaddrs.py - print domain interfaces among with their HW and IP addresses
 
 The XML files in this directory are examples of the XML format that libvirt
 expects, and will have to be adapted for your setup. They are only needed
diff --git a/examples/python/domipaddrs.py b/examples/python/domipaddrs.py
new file mode 100644
index 0000000..fd2d201
--- /dev/null
+++ b/examples/python/domipaddrs.py
@@ -0,0 +1,62 @@
+#!/usr/bin/env python
+# domipaddrds - print IP addresses for running domain
+
+import libxml2
+import libvirt
+import sys
+
+def usage():
+    print "Usage: %s [URI] DOMAIN" % sys.argv[0]
+    print "        Print IP addresses assigned to domain interfaces"
+
+uri = None
+name = None
+args = len(sys.argv)
+
+if args == 2:
+    name = sys.argv[1]
+elif args == 3:
+    uri = sys.argv[1]
+    name = sys.argv[2]
+else:
+    usage()
+    sys.exit(2)
+
+conn = libvirt.openReadOnly(uri)
+if conn == None:
+    print "Unable to open connection to libvirt"
+    sys.exit(1)
+
+try:
+    dom = conn.lookupByName(name)
+except libvirt.libvirtError:
+    print "Domain %s not found" % name
+    sys.exit(0)
+
+ifaces = dom.interfacesAddresses(0)
+if (ifaces == None):
+    print "Failed to get domain interfaces"
+    sys.exit(0)
+
+doc = libxml2.parseDoc(ifaces)
+ctxt = doc.xpathNewContext()
+res = ctxt.xpathEval("/interfaces/interface/name/text()")
+
+print " {0:10} {1:17}    {2}".format("Interface", "HW address", "IP Address")
+
+for interface in res:
+    hwaddr = ctxt.xpathEval("string(/interfaces/interface[descendant::name/text()='%s']/hwaddr/text())" % interface)
+    ip_addrs = ctxt.xpathEval("/interfaces/interface[descendant::name/text()='%s']/addresses/ip" % interface)
+    print " {0:10} {1:17}".format(interface, hwaddr),
+
+    print " ",
+    for ip in ip_addrs:
+        prefix = ""
+        for i in ip.properties:
+            if i.name == "prefix":
+                prefix = i.content
+        print " {0}/{1}".format(ip.content, prefix),
+
+    print
+
+doc.freeDoc()
-- 
1.7.8.5


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]