Created
          June 29, 2018 13:19 
        
      - 
      
- 
        Save marco79cgn/5a01bb996bc3f69f0c79d585461dd33a to your computer and use it in GitHub Desktop. 
    Amazon Dash Button Script
  
        
  
    
      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
    
  
  
    
  | import datetime | |
| import logging | |
| import urllib2 | |
| import time | |
| # Constants | |
| timespan_threshhold = 3 | |
| # Globals | |
| lastpress = datetime.datetime(1970,1,1) | |
| lastpress2 = datetime.datetime(1970,1,1) | |
| lastpress3 = datetime.datetime(1970,1,1) | |
| lastpress4 = datetime.datetime(1970,1,1) | |
| logging.getLogger("scapy.runtime").setLevel(logging.ERROR) | |
| from scapy.all import * | |
| def button_pressed_dash1(): | |
| global lastpress | |
| thistime = datetime.datetime.now() | |
| timespan = thistime - lastpress | |
| if timespan.total_seconds() > timespan_threshhold: | |
| current_time = datetime.datetime.strftime(thistime, '%Y-%m-%d %H:%M:%S') | |
| print 'Dash button radioeins pressed at ' + current_time | |
| urllib2.urlopen('http://192.168.178.62/connair/index.php?action=allon') | |
| time.sleep(2) | |
| urllib2.urlopen('http://192.168.178.62/connair/index.php?action=allon') | |
| time.sleep(2) | |
| urllib2.urlopen('http://192.168.178.62/connair/index.php?action=allon') | |
| time.sleep(50) | |
| urllib2.urlopen('http://192.168.178.62:5005/preset/wakeup') | |
| lastpress = thistime | |
| def button_pressed_dash2(): | |
| global lastpress2 | |
| thistime = datetime.datetime.now() | |
| timespan = thistime - lastpress2 | |
| if timespan.total_seconds() > timespan_threshhold: | |
| current_time = datetime.datetime.strftime(thistime, '%Y-%m-%d %H:%M:%S') | |
| print 'Dash button 80s80s pressed at ' + current_time | |
| urllib2.urlopen('http://192.168.178.62:5005/preset/80s80s') | |
| lastpress2 = thistime | |
| def button_pressed_dash3(): | |
| global lastpress3 | |
| thistime = datetime.datetime.now() | |
| timespan = thistime - lastpress3 | |
| if timespan.total_seconds() > timespan_threshhold: | |
| current_time = datetime.datetime.strftime(thistime, '%Y-%m-%d %H:%M:%S') | |
| print 'Dash button OFF pressed at ' + current_time | |
| urllib2.urlopen('http://192.168.178.62/connair/index.php?action=alloff') | |
| time.sleep(5) | |
| urllib2.urlopen('http://192.168.178.62/connair/index.php?action=alloff') | |
| lastpress3 = thistime | |
| def button_pressed_dash4(): | |
| global lastpress4 | |
| thistime = datetime.datetime.now() | |
| timespan = thistime - lastpress4 | |
| if timespan.total_seconds() > timespan_threshhold: | |
| current_time = datetime.datetime.strftime(thistime, '%Y-%m-%d %H:%M:%S') | |
| print 'Dash button fluxfm90er pressed at ' + current_time | |
| urllib2.urlopen('http://192.168.178.62:5005/preset/fluxfm90er') | |
| lastpress4 = thistime | |
| 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: | |
| # we've found the IP address, which means its the second and final UDP request, so we can trigger our action | |
| mac_to_action[pkt.src]() | |
| break | |
| else: pass | |
| mac_to_action = {'50:f5:da:c5:ef:80' : button_pressed_dash1 , 'fc:a6:67:ab:55:95' : button_pressed_dash2 , 'ac:63:be:5b:ac:0b' : button_pressed_dash3, '78:e1:03:67:a0:27': button_pressed_dash4} | |
| 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() | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment