Skip to content

Instantly share code, notes, and snippets.

@Talon876
Last active October 5, 2016 03:35
Show Gist options
  • Save Talon876/57f093fc98a76634df3a19280385d8cd to your computer and use it in GitHub Desktop.
Save Talon876/57f093fc98a76634df3a19280385d8cd to your computer and use it in GitHub Desktop.
A simple script that monitors DHCP requests from specific MAC addresses and then performs an action. Used to add functionality to Amazon Dash buttons.
#!/usr/bin/env python
from scapy.all import *
import datetime
import door
import logging
logging.getLogger('scapy.runtime').setLevel(logging.ERROR)
# Enter your amazon dash button MACs here
macs = {
'xx:xx:xx:xx:xx:xx': 'some label'
}
def dash_pressed(name):
now = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')
print('{} | {} pressed - toggling door!'.format(now, name))
door.toggle()
def process(pkt):
dash_pressed(macs[pkt.src])
def dash_filter(pkt):
return pkt.src in macs and DHCP in pkt
if __name__=='__main__':
print('Listening for DHCP requests from dash buttons: {}'.format(','.join(macs.keys())))
sniff(filter='udp', lfilter=dash_filter, prn=process)
#!/usr/bin/env python
import os
import requests
import json
particle_config = json.load(open('/opt/scripts/.secrets.json'))['particle.io']
token = particle_config['token']
device_id = particle_config['device_id']
if not token:
print('You need to set your access token in secrets.json')
def toggle():
if token != 'DRY_RUN':
try:
requests.post('https://api.particle.io/v1/devices/{}/toggle'.format(device_id), params={
'access_token': token
})
except:
print('Failed to toggle')
else: print('(not really) toggling garage door')
if __name__=='__main__':
toggle()
{
"particle.io": {
"token": "2c17d5...<your particle token>",
"device_id": "your_particle_io_device_name"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment