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

Michal Privoznik mprivozn at redhat.com
Fri Jun 8 08:04:38 UTC 2012


---
 examples/python/Makefile.am   |    2 +-
 examples/python/README        |    1 +
 examples/python/domipaddrs.py |   50 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 52 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..74c0716
--- /dev/null
+++ b/examples/python/domipaddrs.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python
+# domipaddrds - print IP addresses for running domain
+
+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)
+
+print " {0:10} {1:17}    {2}".format("Interface", "HW address", "IP Address")
+
+for (name, val) in ifaces.iteritems():
+    print " {0:10} {1:17}".format(name, val['hwaddr']),
+
+    if (val['ip_addrs'] <> None):
+        print "  ",
+        for addr in val['ip_addrs']:
+            print "{0}/{1} ".format(addr['addr'], addr['prefix']),
+
+    print
-- 
1.7.8.5




More information about the libvir-list mailing list