d32 d32 - 2 months ago 26
Python Question

python dash button trigger not working

this is my script to see if the amazon dash button is pressed:

from scapy.all import *

def udp_filter(pkt):
options = pkt[DHCP].options
for option in options:
if isinstance(option, tuple):
if 'requested_addr' in option:
print('button pressed')
break

print('Waiting for a button press...')
sniff(prn=udp_filter, store=0, count=0, filter="udp", lfilter=lambda d: d.src == '8c:xx:xx:xx:xx:xx')

if __name__ == "__main__":
main()


but i got the following error message:

Traceback (most recent call last):
File "short.py", line 12, in <module>
sniff(prn=udp_filter, store=0, count=0, filter="udp", lfilter=lambda d: d.src == '8c:xx:xx:xx:xx:xx')
File "/usr/local/lib/python3.4/dist-packages/scapy/sendrecv.py", line 597, in sniff
r = prn(p)
File "dash.py", line 4, in udp_filter
options = pkt[DHCP].options
File "/usr/local/lib/python3.4/dist-packages/scapy/packet.py", line 814, in __getitem__
raise IndexError("Layer [%s] not found" % lname)
IndexError: Layer [DHCP] not found


has someone a idea how to fix it and get it working under python3.4?

Answer

It looks like the error is saying it can't find a DHCP Layer in the packet. Could you add if pkt.haslayer(DHCP): just above options = pkt[DHCP].options like this:

def udp_filter(pkt): if pkt.haslayer(DHCP): options = pkt[DHCP].options for option in options: if isinstance(option, tuple): if 'requested_addr' in option: print('button pressed') break else: pass
That way it will check for DHCP before looking for pkt[DHCP].options.