From: Nehal J Wani
This API returns a list of DHCP leases for all network interfaces
connected to the given virtual network or limited output just for one
interface if mac is specified.
Example Output:
[{'iface': 'virbr3', 'ipaddr': '192.168.150.181', 'hostname': 'ubuntu14',
'expirytime': 1403737495L, 'prefix': 24, 'clientid': None,
'mac': '52:54:00:e8:73:eb', 'iaid': None, 'type': 0},
{'iface': 'virbr3', 'ipaddr': '2001:db8:ca2:2:1::bd', 'hostname':
'fedora20-test',
'expirytime': 1403738587L, 'prefix': 64, 'clientid':
'00:04:b1:d8:86:42:e1:6a:aa:cf:d5:86:94:23:6f:94:04:cd',
'mac': '52:54:00:5b:40:98', 'iaid': '5980312', 'type': 1}]
Signed-off-by: Peter Krempa
---
examples/README | 1 +
examples/dhcpleases.py | 53 +
generator.py | 4 +++
libvirt-override-api.xml | 7
libvirt-override.c | 88
sanitytest.py| 6
6 files changed, 159 insertions(+)
create mode 100755 examples/dhcpleases.py
diff --git a/examples/README b/examples/README
index f4db76c..5b5d405 100644
--- a/examples/README
+++ b/examples/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
+dhcpleases.py - list dhcp leases for a given virtual network
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/dhcpleases.py b/examples/dhcpleases.py
new file mode 100755
index 000..c172dc2
--- /dev/null
+++ b/examples/dhcpleases.py
@@ -0,0 +1,53 @@
+#!/usr/bin/env python
+# netdhcpleases - print leases info for given virtual network
+
+import libvirt
+import sys
+import time
+
+def usage():
+print "Usage: %s [URI] NETWORK" % sys.argv[0]
+print "Print leases info for a given virtual network"
+
+uri = None
+network = None
+args = len(sys.argv)
+
+if args == 2:
+network = sys.argv[1]
+elif args == 3:
+uri = sys.argv[1]
+network = 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:
+net = conn.networkLookupByName(network)
+except libvirt.libvirtError:
+print "Network %s not found" % network
+sys.exit(0)
+
+leases = net.DHCPLeases();
+if (leases == None):
+print "Failed to get leases for %s" % net.name()
+sys.exit(0)
+
+def toIPAddrType(addrType):
+if addrType == libvirt.VIR_IP_ADDR_TYPE_IPV4:
+return "ipv4"
+elif addrType == libvirt.VIR_IP_ADDR_TYPE_IPV6:
+return "ipv6"
+
+print " {0:20} {1:18} {2:9} {3:25} {4:15} {5}".format("Expiry Time", "MAC
address", "Protocol", "IP address", "Hostname", "Client ID or DUID")
+print "-"*115
+
+for lease in leases:
+print " {0:20}".format(time.strftime('%Y-%m-%d %H:%M:%S',
time.localtime(lease['expirytime']))),
+print "{0:18} {1:9}".format(lease['mac'], toIPAddrType(lease['type'])),
+print "{0:<25} {1:15} {2}".format("{}/{}".format(lease['ipaddr'],
lease['prefix']), lease['hostname'], lease['clientid'])
diff --git a/generator.py b/generator.py
index 03027c6..a12c52b 100755
--- a/generator.py
+++ b/generator.py
@@ -463,6 +463,7 @@ skip_impl = (
'virDomainMigrateToURI3',
'virConnectGetCPUModelNames',
'virNodeGetFreePages',
+'virNetworkGetDHCPLeases',
)
lxc_skip_impl = (
@@ -568,6 +569,8 @@ skip_function = (
"virTypedParamsGetString",
"virTypedParamsGetUInt",
"virTypedParamsGetULLong",
+
+'virNetworkDHCPLeaseFree', # only useful in C, python code uses list
)
lxc_skip_function = (
@@ -1115,6 +1118,7 @@ def nameFixup(name, classe, type, file):
elif name[0:13] == "virNetworkGet":
func = name[13:]
func = func[0:1].lower() + func[1:]
+func = func.replace("dHCP", "DHCP")
elif name[0:10] == "virNetwork":
func = name[10:]
func = func[0:1].lower() + func[1:]
diff --git a/libvirt-override-api.xml b/libvirt-override-api.xml
index bbf0ab1..09bbbf8 100644
--- a/libvirt-override-api.xml
+++ b/libvirt-override-api.xml
@@ -633,5 +633,12 @@
+
+ Returns a list of dhcp leases for interfaces connected to the
given virtual network
+
+
+
+
+
diff --git a/libvirt-override.c b/libvirt-override.c
index 40aefcc..ed5e9e4 100644
--- a/libvirt-override.c
+++ b/libvirt-override.c
@@ -7866,6 +7866,93 @@ libvirt_virNodeGetFreePages(PyObject *self
ATTRIBUTE_UNUSED,
VIR_FREE(counts);
return py_retval;
}
+
+
+static PyObject *
+libvirt_virNetworkGetDHCPLeases(PyObject *self ATTRIBUTE_UNUSED,
+PyObject *args)
+{
+PyObject *py_ret