-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpresence_scapy.py
57 lines (44 loc) · 1.24 KB
/
presence_scapy.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
#!/usr/bin/python
'''
Requirements:
python 2.7
pip install scapy
Give python high privileges (scapy requires root privileges):
sudo setcap cap_net_raw=eip /usr/bin/python2.7
'''
import subprocess
from time import sleep
import paho.mqtt.publish as publish
import string, sys
from scapy.all import *
conf.verb = 0
#=========================== SETTINGS ===========================
MQTT_HOST = "127.0.0.1"
MQQT_USER = "mosquitto"
MQTT_PASS = "12345678"
MQTT_TOPIC = "presence"
T_SLEEP = 60
IP_RANGE = "10.0.0.1/24"
DEVICES= {"Bob": "00:00:00:00:00:00", "Alice":"01:01:01:01:01:01"}
#========================= END SETTINGS =========================
auth = {
'username':MQQT_USER,
'password':MQTT_PASS
}
while True:
try:
ans,unans=srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=IP_RANGE),timeout=2)
tmpMACS = {}
for s,r in ans:
tmpMACS[r.src] = True
for device in DEVICES:
if DEVICES[device].lower() in tmpMACS:
print(device + " is home")
publish.single(MQTT_TOPIC + "/" + device,"home",hostname=MQTT_HOST, auth=auth)
else:
print(device + " is not_home")
publish.single(MQTT_TOPIC + "/" + device,"not_home",hostname=MQTT_HOST, auth=auth)
except Exception, e:
print e
print "Something went wrong!"
sleep(T_SLEEP)