Skip to content

Instantly share code, notes, and snippets.

@hkwi
Last active February 3, 2017 09:13
Show Gist options
  • Save hkwi/8aace8228121fc4fc991872fbbcf0f05 to your computer and use it in GitHub Desktop.
Save hkwi/8aace8228121fc4fc991872fbbcf0f05 to your computer and use it in GitHub Desktop.
dnsmasq dhcp lease watch by dbus signals
import dbus
dnsmasq = dbus.SystemBus().get_object("uk.org.thekelleys.dnsmasq", "/uk/org/thekelleys/dnsmasq")
dnsmasq.AddDhcpLease("192.168.0.12","00:11:22:33:44:55",dbus.ByteArray(),dbus.ByteArray(),dbus.UInt32(5),dbus.UInt32(0),False)
dnsmasq.DeleteDhcpLease("192.168.0.12")
import dbus
import functools
from gi.repository import GLib
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)
dnsmasq = dbus.SystemBus().get_object("uk.org.thekelleys.dnsmasq", "/uk/org/thekelleys/dnsmasq")
def func(*args, **kwargs):
print(args, kwargs)
for m in ("DhcpLeaseAdded", "DhcpLeaseDeleted", "DhcpLeaseUpdated"):
dnsmasq.connect_to_signal(m, functools.partial(func, m))
GLib.MainLoop().run()
Vagrant.configure("2") do |config|
config.vm.box = "minimal/xenial64"
config.vm.provision "file", source: "listener.py", destination: "listener.py"
config.vm.provision "file", source: "hook.py", destination: "hook.py"
config.vm.provision "shell", inline: <<-SHELL
apt-get update -y
apt-get install -y python-dbus dnsmasq libglib2.0-bin
echo "enable-dbus" >> /etc/dnsmasq.d/dbus.conf
echo "dhcp-range=192.168.0.2,192.168.0.31" >> /etc/dnsmasq.d/dbus.conf
systemctl restart dnsmasq
SHELL
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment