Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. import socket
  2. import struct
  3. import binascii
  4. import time
  5. import json
  6. import urllib2
  7.  
  8. # Use your own IFTTT key
  9. ifttt_key = 'xxxxxxxx'
  10. # Set these up at https://ifttt.com/maker
  11. ifttt_url_button = 'https://maker.ifttt.com/trigger/button_pressed/with/key/' + ifttt_key
  12.  
  13. # Replace this MAC addresses and nickname with your own
  14. macs = {
  15. '50F5DA088C82' : 'vanish'
  16. }
  17.  
  18. # Trigger a IFTTT URL. Body includes JSON with timestamp values.
  19. def trigger_url(url):
  20. data = '{ "value1" : "' + time.strftime("%Y-%m-%d") + '", "value2" : "' + time.strftime("%H:%M") + '" }'
  21. req = urllib2.Request(url, data, {'Content-Type': 'application/json'})
  22. f = urllib2.urlopen(req)
  23. response = f.read()
  24. f.close()
  25. return response
  26.  
  27. def button_pressed():
  28. print 'triggering button event, response: ' + trigger_url(ifttt_url_button)
  29.  
  30. rawSocket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.htons(0x0003))
  31.  
  32. while True:
  33. packet = rawSocket.recvfrom(2048)
  34. ethernet_header = packet[0][0:14]
  35. ethernet_detailed = struct.unpack("!6s6s2s", ethernet_header)
  36. # skip non-ARP packets
  37. ethertype = ethernet_detailed[2]
  38. if ethertype != '\x08\x06':
  39. continue
  40. # read out data
  41. arp_header = packet[0][14:42]
  42. arp_detailed = struct.unpack("2s2s1s1s2s6s4s6s4s", arp_header)
  43. source_mac = binascii.hexlify(arp_detailed[5])
  44. source_ip = socket.inet_ntoa(arp_detailed[6])
  45. dest_ip = socket.inet_ntoa(arp_detailed[8])
  46. if source_mac in macs:
  47. #print "ARP from " + macs[source_mac] + " with IP " + source_ip
  48. if macs[source_mac] == 'vanish':
  49. button_pressed()
  50. else:
  51. print "Unknown MAC " + source_mac + " from IP " + source_ip
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement