Skip to content

Instantly share code, notes, and snippets.

@mr-pj
Last active January 3, 2018 12:34
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mr-pj/75297864abef5c8f2d5c134be2656023 to your computer and use it in GitHub Desktop.
Save mr-pj/75297864abef5c8f2d5c134be2656023 to your computer and use it in GitHub Desktop.
if dhcp request matches a given mac, an action will be taken. Requires pydhcplib
from pydhcplib.dhcp_network import *
def do_something():
print("button has been pressed")
netopt = {'client_listen_port':"68", 'server_listen_port':"67", 'listen_address':"0.0.0.0"}
class Server(DhcpServer):
def __init__(self, options, dashbuttons):
DhcpServer.__init__(self, options["listen_address"],
options["client_listen_port"],
options["server_listen_port"])
self.dashbuttons = dashbuttons
def HandleDhcpRequest(self, packet):
mac = self.hwaddr_to_str(packet.GetHardwareAddress())
self.dashbuttons.press(mac)
def hwaddr_to_str(self, hwaddr):
result = []
hexsym = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f']
for iterator in range(6) :
result += [str(hexsym[hwaddr[iterator]/16]+hexsym[hwaddr[iterator]%16])]
return ':'.join(result)
class DashButtons():
def __init__(self):
self.buttons = {}
def register(self, mac, function):
self.buttons[mac] = function
def press(self, mac):
if mac in self.buttons:
self.buttons[mac]()
return True
return False
dashbuttons = DashButtons()
dashbuttons.register("50:f4:de:f1:3b:a0", do_something)
server = Server(netopt, dashbuttons)
while True :
server.GetNextDhcpPacket()
@agrestic1
Copy link

works fine, thank you!

Just a hint for usesrs: You have to write the MAC adresses using lowercase letters (like the excemple)

@tikismoke
Copy link

Thank's works great here too with latest dash button (meaning new firmware)

@benmargolin
Copy link

Thanks much for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment