This is an adaptation of something I found over 12 months ago. If it looks familiar, please say as I'm more than happy to credit.
#!/usr/bin/env python
from scapy.all import *
import datetime
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
def button_pressed_dash():
print 'Dash button pressed at %s' % datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')
def udp_filter(pkt):
options = pkt[DHCP].options
for option in options:
if isinstance(option, tuple) and 'requested_addr' in option:
mac_to_action[pkt.src]()
break
mac_to_action = {'xx:xx:xx:xx:xx:xx' : button_pressed_dash}
mac_id_list = list(mac_to_action.keys())
print "Waiting for a button press..."
sniff(prn=udp_filter, store=0, filter="udp", lfilter=lambda d: d.src in mac_id_list)
if __name__ == "__main__":
main()
To run:
$ sudo ./pydash.py