Created
September 15, 2018 21:14
-
-
Save bfagundez/1f4cbd6b1e3ea4e7f9c92b931e00817f to your computer and use it in GitHub Desktop.
sniff dash buttons
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from __future__ import print_function | |
| from pydhcplib.dhcp_network import * | |
| from googleapiclient.discovery import build | |
| from httplib2 import Http | |
| from oauth2client import file, client, tools | |
| from datetime import datetime | |
| SPREADSHEET_ID = '1iiGsl0ZElGXD1p6aLFD2GB1oT39xQqV7LVqnGRjxlIc' | |
| def append_to_spreadsheet(data): | |
| print("button has been pressed, sending data") | |
| print(data) | |
| store = file.Storage('token.json') | |
| creds = store.get() | |
| if not creds or creds.invalid: | |
| flow = client.flow_from_clientsecrets('credentials.json', SCOPES) | |
| creds = tools.run_flow(flow, store) | |
| service = build('sheets', 'v4', http=creds.authorize(Http())) | |
| values = [data] | |
| body = { 'values': values } | |
| result = service.spreadsheets().values().append( | |
| spreadsheetId=SPREADSHEET_ID, range='A1:B1', | |
| valueInputOption='USER_ENTERED', body=body).execute() | |
| print('{0} cells appended.'.format(result \ | |
| .get('updates') \ | |
| .get('updatedCells'))); | |
| def log_poop(): | |
| append_to_spreadsheet(['Poop', datetime.now().strftime('%c')]) | |
| def log_pee(): | |
| append_to_spreadsheet(['Pee', datetime.now().strftime('%c')]) | |
| 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("fc:65:de:2a:87:7a", log_poop) | |
| dashbuttons.register("6c:56:97:5f:e8:a2", log_pee) | |
| 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