mirror of
https://github.com/KevinMidboe/homeChecker.git
synced 2025-10-29 17:40:26 +00:00
Created homeSender for com with others on the network, this will be changed!
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1 @@
|
|||||||
times.txt
|
__pycache__
|
||||||
|
|||||||
@@ -1,23 +1,19 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
from socket import *
|
from socket import *
|
||||||
|
from macLookup import getTimes
|
||||||
|
import json
|
||||||
|
|
||||||
# Define the socket communicating will transfered
|
# Define the socket communicating will transfered
|
||||||
serverSocket = socket(AF_INET, SOCK_DGRAM)
|
serverSocket = socket(AF_INET, SOCK_DGRAM)
|
||||||
serverSocket.bind(('', 12001))
|
serverSocket.bind(('', 12001))
|
||||||
|
|
||||||
def readFile():
|
|
||||||
with open('/home/kevin/homeCheck/times.txt', 'r') as content_file:
|
|
||||||
content = content_file.read()
|
|
||||||
return content
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
# Save the message and return address to variables.
|
# Save the message and return address to variables.
|
||||||
message, address = serverSocket.recvfrom(1024)
|
message, address = serverSocket.recvfrom(1024)
|
||||||
|
|
||||||
print(message, address) # Debug print
|
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.
|
# This should prob be cases and not if's, but works to get the right info user requests.
|
||||||
if (message == 'get_times'):
|
serverSocket.sendto(bytes(times, 'utf-8'), address)
|
||||||
return_message = readFile()
|
|
||||||
|
|
||||||
# Returns message to return address.
|
|
||||||
serverSocket.sendto(return_message, address)
|
|
||||||
12
macLookup.py
12
macLookup.py
@@ -7,6 +7,8 @@ from re import findall
|
|||||||
from sys import argv
|
from sys import argv
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
|
|
||||||
|
path = "/home/kevin/homeChecker/home.db"
|
||||||
|
|
||||||
def getOnlineClients():
|
def getOnlineClients():
|
||||||
try:
|
try:
|
||||||
arpOutput = check_output("sudo arp-scan -l", shell=True)
|
arpOutput = check_output("sudo arp-scan -l", shell=True)
|
||||||
@@ -25,7 +27,7 @@ def getAddr(c):
|
|||||||
return [i[0] for i in c.fetchall()]
|
return [i[0] for i in c.fetchall()]
|
||||||
|
|
||||||
def getTimes():
|
def getTimes():
|
||||||
conn = sqlite3.connect('home.db')
|
conn = sqlite3.connect(path)
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
|
|
||||||
c.execute('SELECT c.name, l.timesince FROM lastonline AS l JOIN clients AS c WHERE l.clientadr=c.adr')
|
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():
|
def updateTimes():
|
||||||
curTime = time()
|
curTime = time()
|
||||||
conn = sqlite3.connect('home.db')
|
conn = sqlite3.connect(path)
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
|
|
||||||
online = list(set(getOnlineClients()) & set(getAddr(c)))
|
online = list(set(getOnlineClients()) & set(getAddr(c)))
|
||||||
@@ -73,12 +75,10 @@ def updateTimes():
|
|||||||
return (online)
|
return (online)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
if argv[-1] == 'add':
|
if argv[-1] == 'get':
|
||||||
print("Updated following clients:", updateTimes())
|
|
||||||
elif argv[-1] == 'get':
|
|
||||||
pprint(getTimes())
|
pprint(getTimes())
|
||||||
else:
|
else:
|
||||||
print("Add args 'add' or 'get'")
|
print("Updated following clients:", updateTimes())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user