diff --git a/.gitignore b/.gitignore index ccac1c8..bee8a64 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -times.txt +__pycache__ diff --git a/home.db b/home.db index 874f39a..a950d28 100644 Binary files a/home.db and b/home.db differ diff --git a/readFileHandler.py b/homeSender.py similarity index 58% rename from readFileHandler.py rename to homeSender.py index d166047..5931484 100755 --- a/readFileHandler.py +++ b/homeSender.py @@ -1,23 +1,19 @@ +#!/usr/bin/env python3 from socket import * +from macLookup import getTimes +import json # Define the socket communicating will transfered serverSocket = socket(AF_INET, SOCK_DGRAM) serverSocket.bind(('', 12001)) -def readFile(): - with open('/home/kevin/homeCheck/times.txt', 'r') as content_file: - content = content_file.read() - return content - while True: # Save the message and return address to variables. message, address = serverSocket.recvfrom(1024) print(message, address) # Debug print - + + times = json.dumps(getTimes()) # This should prob be cases and not if's, but works to get the right info user requests. - if (message == 'get_times'): - return_message = readFile() + serverSocket.sendto(bytes(times, 'utf-8'), address) - # Returns message to return address. - serverSocket.sendto(return_message, address) diff --git a/macLookup.py b/macLookup.py index 1344f9b..103907c 100755 --- a/macLookup.py +++ b/macLookup.py @@ -7,6 +7,8 @@ from re import findall from sys import argv from pprint import pprint +path = "/home/kevin/homeChecker/home.db" + def getOnlineClients(): try: arpOutput = check_output("sudo arp-scan -l", shell=True) @@ -25,7 +27,7 @@ def getAddr(c): return [i[0] for i in c.fetchall()] def getTimes(): - conn = sqlite3.connect('home.db') + conn = sqlite3.connect(path) c = conn.cursor() c.execute('SELECT c.name, l.timesince FROM lastonline AS l JOIN clients AS c WHERE l.clientadr=c.adr') @@ -59,7 +61,7 @@ def convertTime(seconds): def updateTimes(): curTime = time() - conn = sqlite3.connect('home.db') + conn = sqlite3.connect(path) c = conn.cursor() online = list(set(getOnlineClients()) & set(getAddr(c))) @@ -73,12 +75,10 @@ def updateTimes(): return (online) if __name__ == '__main__': - if argv[-1] == 'add': - print("Updated following clients:", updateTimes()) - elif argv[-1] == 'get': - pprint(getTimes()) + if argv[-1] == 'get': + pprint(getTimes()) else: - print("Add args 'add' or 'get'") + print("Updated following clients:", updateTimes())