Created
July 1, 2018 22:37
-
-
Save garethnunns/1a3a39ae8a15358e3ffb8f928ce865e6 to your computer and use it in GitHub Desktop.
Logs to CSV the RSS value perceived by the Raspberry Pi of devices connected to its WiFi AP
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
""" | |
File: WiFiLog.py | |
Author: garethnunns.com | |
Logs to CSV the RSS value perceived by the Raspberry Pi of devices connected to its WiFi AP | |
""" | |
import subprocess, csv, time, threading | |
filename = raw_input('Enter file name: ') | |
def log(filename): | |
with open(filename+'.csv', 'a') as csvfile: | |
csvwriter = csv.writer(csvfile) | |
# get mac addresses of connected devices | |
procMAC = subprocess.Popen("iw dev wlan0 station dump | grep 'Station' | awk '{print $2}'", | |
shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
MACs = [] | |
for line in procMAC.stdout.readlines(): | |
MACs.append(line.strip()) | |
sorted(MACs) | |
csvwriter.writerow([time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())] + MACs) | |
print MACs | |
# get IPs from MACs | |
ipCommand = "&&".join(["ip neigh | grep '"+MAC+"' | awk '{print $1}'" for MAC in MACs]) | |
procIP = subprocess.Popen(ipCommand,shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
IPs = [] | |
for line in procIP.stdout.readlines(): | |
IPs.append(line.strip()) | |
csvwriter.writerow([time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())] + IPs) | |
print IPs | |
# ping connected devices (updates RSS as no inactive time) | |
pingCommand = "&&".join(["ping -c 1 "+IP for IP in IPs]) | |
subprocess.Popen(pingCommand,shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
# get signal levels | |
signals = [] | |
sigCommand = "&&".join(["iw dev wlan0 station get '"+MAC+"' | grep 'signal' | awk '{print $2}'" for MAC in MACs]) | |
procSig = subprocess.Popen(sigCommand, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
for line in procSig.stdout.readlines(): | |
signals.append(line.strip()) | |
print signals | |
csvwriter.writerow([time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())] + signals) | |
step = True | |
while True: | |
step = not step | |
if step: | |
print "step" | |
t = threading.Thread(target=log,args=[filename]) | |
t.start() | |
time.sleep(2.5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment