-
Notifications
You must be signed in to change notification settings - Fork 9
/
doorbell.py
executable file
·61 lines (51 loc) · 1.76 KB
/
doorbell.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python
import os
import signal
import subprocess
import sys
import time
# Ignore SIGCHLD
# This will prevent zombies
signal.signal(signal.SIGCHLD, signal.SIG_IGN)
# After ringing, how many seconds before we allow ourselves to ring again
DEBOUNCE_INTERVAL = 7
# The token to look for in tcpdump's output
# This should be your unique network SSID
# SSID_TOKEN = sys.argv[1] if len(sys.argv) > 1 else 'Free Public Wifi'
# This file holds a list of SSID's to look for in tcpdump's output
# Each SSID is delineated by a new line
# White space at the beginning and end of each line is stripped out before processing
f = open("dash_ssids")
lines = f.readlines()
f.close()
SSID_TOKENS = []
for line in lines:
SSID_TOKENS.append(line.strip())
print "Number of tokens in list:"
print len(SSID_TOKENS)
print "SSID_TOKENS is/are:"
for SSID_TOKEN_PRINT in SSID_TOKENS:
print SSID_TOKEN_PRINT
DEVNULL = open(os.devnull, 'wb')
def do_ring():
""" Play the doorbell.wav file. Don't wait for it to finish. """
cmd = 'alsaplayer -o alsa --quiet ./doorbell.wav'
soundproc = subprocess.Popen(cmd.split(), close_fds=True,
stdin=DEVNULL, stdout=DEVNULL, stderr=DEVNULL)
cmd = 'tcpdump -l -K -q -i DoorbellMonitor -n -s 256'
proc = subprocess.Popen(cmd.split(), close_fds=True,
bufsize=0, stdout=subprocess.PIPE)
last_played = 0
while True:
line = proc.stdout.readline()
if not line:
print "tcpdump exited"
break
for SSID_TOKEN in SSID_TOKENS:
if SSID_TOKEN in line:
now = time.time()
if now - last_played > DEBOUNCE_INTERVAL:
last_played = now
sys.stdout.write(line)
sys.stdout.flush()
do_ring()