diff options
| author | Pavel Hrdina <phrdina@redhat.com> | 2015-03-28 11:21:56 +0100 |
|---|---|---|
| committer | Pavel Hrdina <phrdina@redhat.com> | 2015-03-28 11:21:56 +0100 |
| commit | 0be1f5e31a06af9d93d5de73242de7f263cc5c3d (patch) | |
| tree | 5f360af7781f543dbecc4f8047419d9d53033107 /examples | |
| parent | e1e9b270965a94ce703d9cb2a458824877a77cf0 (diff) | |
| download | libvirt-python-0be1f5e31a06af9d93d5de73242de7f263cc5c3d.tar.gz | |
Expose virDomainInterfacesAddresses to python bindingv1.2.14
examples/Makefile.am:
* Add new file domipaddrs.py
examples/README:
* Add documentation for the python example
libvirt-override-api.xml:
* Add new symbol for virDomainInterfacesAddresses
libvirt-override.c:
* Hand written python api
Example:
$ python examples/domipaddrs.py qemu:///system f18
Interface MAC address Protocol Address
vnet0 52:54:00:20:70:3d ipv4 192.168.105.240/16
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/README | 1 | ||||
| -rwxr-xr-x | examples/domipaddrs.py | 57 |
2 files changed, 58 insertions, 0 deletions
diff --git a/examples/README b/examples/README index 5b5d405..1d4b425 100644 --- a/examples/README +++ b/examples/README @@ -11,6 +11,7 @@ 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 dhcpleases.py - list dhcp leases for a given virtual network +domipaddrs.py - list IP addresses for guest domains 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/domipaddrs.py b/examples/domipaddrs.py new file mode 100755 index 0000000..d6d5cac --- /dev/null +++ b/examples/domipaddrs.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python +# domipaddrs - print domain interfaces along with their MAC and IP addresses + +import libvirt +import sys + +def usage(): + print "Usage: %s [URI] DOMAIN" % sys.argv[0] + print " Print domain interfaces along with their MAC and IP addresses" + +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.open(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.interfaceAddresses(libvirt.VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE); +if (ifaces == None): + print "Failed to get domain interfaces" + sys.exit(0) + +print " {0:10} {1:20} {2:12} {3}".format("Interface", "MAC address", "Protocol", "Address") + +def toIPAddrType(addrType): + if addrType == libvirt.VIR_IP_ADDR_TYPE_IPV4: + return "ipv4" + elif addrType == libvirt.VIR_IP_ADDR_TYPE_IPV6: + return "ipv6" + +for (name, val) in ifaces.iteritems(): + if val['addrs']: + for addr in val['addrs']: + print " {0:10} {1:19}".format(name, val['hwaddr']), + print " {0:12} {1}/{2} ".format(toIPAddrType(addr['type']), addr['addr'], addr['prefix']), + print + else: + print " {0:10} {1:19}".format(name, val['hwaddr']), + print " {0:12} {1}".format("N/A", "N/A"), + print |
