Package: dhcp-leases
Small python module for reading /var/lib/dhcp/dhcpd.leases from
isc-dhcp-server. This module works in Python 2.7 and 3.xThis module also supports reading lease files from the isc dhcp daemon
running in IPv6 mode (Since version 0.4.0).InstallationThrough pypi$sudopipinstalldhcp-leasesThrough git$gitclonegit@github.com:acikogun/python-dhcp-leases.git$cdpython-dhcp-leases$pythonsetup.pybuild$sudopythonsetup.pyinstallUsagefromdhcp_leasesimportDhcpLeasesleases=DhcpLeases('/path/to/dhcpd.leases')leases.get()# Returns the leases as a list of Lease objectsleases.get_current()# Returns only the currently valid dhcp leases as dict# The key of the dict is the device mac address and the# Value is a Lease objectOr read a gzip’ed file:fromdhcp_leasesimportDhcpLeases# DhcpLeases(filename, gzip=False)leases=DhcpLeases('/path/to/dhcpd.leases',True)# True param starts the gzip readerleases.get()# Returns the leases as a list of Lease objectsleases.get_current()# Returns only the currently valid dhcp leases as dict# The key of the dict is the device mac address and the# Value is a Lease objectThe Lease object has the following fields (only for IPv4 leases):leaseinstanceofLeaselease.ip# The ip address assigned by this lease as stringlease.ethernet# The mac address of the leaselease.hardware# The OSI physical layer used to request the lease (usually ethernet)lease.start# The start time of this lease as DateTime objectlease.end# The time this lease expires as DateTime object or None if this is an infinite leaselease.hostname# The hostname for this lease if given by the clientlease.binding_state# The binding state as string ('active', 'free', 'abandoned', 'backup')lease.data# Dict of all the info in the dhcpd.leases file for this leaselease.valid# True if the lease hasn't expired and is not in the futurelease.active# True if the binding state is activelease.options# List of extra options in the lease filelease.sets# List of the 'set' items in the lease fileThe Lease6 object has the following fields (only for IPv6):leaseinstanceofLease6lease.ip# The ip address assigned by this lease as stringlease.type# If this is a temporary or permanent address. I's one of the following:# Lease6.TEMPORARY: Temporary lease# Lease6.NON_TEMPORARY: Non-temporary lease# Lease6.PREFIX_DELEGATION: Delegated prefix leaselease.host_identifier# The unique host identifier (replaces mac addresses in IPv6) as byteslease.host_identifier_string# The host_identifier property formatted as an hexadecimal stringlease.duid# The DHCP Unique Identifier (DUID) of the host as byteslease.iaid# The Interface Association Identifier (IAID) of the hostlease.last_communication# The last communication time with the hostlease.end# The time this lease expires as DateTime object or None if this is an infinite leaselease.binding_state# The binding state as string ('active', 'free', 'abandoned', 'backup')lease.preferred_life# The preferred lifetime in secondslease.max_life# The valid lifetime for this address in secondslease.options# List of extra options in the lease filelease.sets# List of the 'set' items in the lease filelease.data# Dict of all the info in the dhcpd6.leases file for this leaseUnit testsThe unit tests can be run withsetup.py:$python3setup.pytest# With coverage report:$coveragerunsetup.pytest