Skip to content

Instantly share code, notes, and snippets.

@mavenius
Forked from mr-pj/dashbutton.py
Created September 11, 2016 16:32
Show Gist options
  • Save mavenius/65a6d5c78acfda88e518f5f3f2cd9b9b to your computer and use it in GitHub Desktop.
Save mavenius/65a6d5c78acfda88e518f5f3f2cd9b9b 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment