mirror of
				https://github.com/KevinMidboe/homeChecker.git
				synced 2025-10-29 17:40:26 +00:00 
			
		
		
		
	Added som test for querying the db.
This commit is contained in:
		
							
								
								
									
										84
									
								
								macLookup.py
									
									
									
									
									
								
							
							
						
						
									
										84
									
								
								macLookup.py
									
									
									
									
									
								
							| @@ -1,44 +1,66 @@ | |||||||
| #!/usr/bin/python | #!/usr/bin/env python3 | ||||||
|  |  | ||||||
| import subprocess | import subprocess | ||||||
| from time import time, sleep | from time import time, sleep | ||||||
|  | import sqlite3 | ||||||
|  |  | ||||||
|  | def getAddr(): | ||||||
|  |     c.execute('SELECT adr FROM clients') | ||||||
|  |     for adr in c.fetchall(): | ||||||
|  |         print(adr[0]) | ||||||
|  |  | ||||||
|  | def getTimes(): | ||||||
|  |     c.execute('SELECT c.name, l.timesince FROM lastonline AS l JOIN clients AS c WHERE l.clientadr=c.adr') | ||||||
|  |     print(c.fetchall()) | ||||||
|  |  | ||||||
|  | def checkNameExistance(): | ||||||
|  |     c.execute('SELECT name, addr FROM macAddr') | ||||||
|  |     test = c.fetchall() | ||||||
|  |     for name, addr in test: | ||||||
|  |         print(name) | ||||||
|  |  | ||||||
| # Define the users to lookup | # Define the users to lookup | ||||||
| users = ['Elias', 'Kevin', 'Nora', 'Inge', 'Bazzinga'] | addr = { "elias": '38:ca:da:eb:3f:da', "kevin": '2c:33:61:aa:e6:a9', "nora": 'cc:29:f5:b8:2d:a2', | ||||||
| addr = ['38:ca:da:eb:3f:da', '2c:33:61:aa:e6:a9', 'cc:29:f5:b8:2d:a2', 'ac:5f:3e:28:2a:c0', 'f0:79:59:70:a4:a6'] |     "inge": 'ac:5f:3e:28:2a:c0', "bazzinga": 'f0:79:59:70:a4:a6' } | ||||||
|  |  | ||||||
|  | conn = sqlite3.connect('home.db') | ||||||
|  | c = conn.cursor() | ||||||
|  |  | ||||||
| def whosHome(): | getAddr() | ||||||
| 	# Get the output of the command 'arp-scan -l' | getTimes() | ||||||
|     arpOutput = subprocess.check_output("sudo arp-scan -l", shell=True).split('\n') | # checkNameExistance() | ||||||
|     # Strip away first three lines and last 5 lines |  | ||||||
|     arpOutput = arpOutput[2:-4]  |  | ||||||
|  |  | ||||||
|     # Open file times.txt and read lines to 'logFile' | # def whosHome(): | ||||||
|     with open('/home/kevin/homeCheck/times.txt', 'r') as file: | # 	# Get the output of the command 'arp-scan -l' | ||||||
| 	    logFile = file.readlines() | #     arpOutput = subprocess.check_output("sudo arp-scan -l", shell=True).split('\n') | ||||||
|  | #     # Strip away first three lines and last 5 lines | ||||||
|  | #     arpOutput = arpOutput[2:-4]  | ||||||
|  |  | ||||||
|     i = 0 | #     # Open file times.txt and read lines to 'logFile' | ||||||
|     # Go through each element in list 'addr' | #     with open('/home/kevin/homeCheck/times.txt', 'r') as file: | ||||||
|     for mac in addr: | # 	    logFile = file.readlines() | ||||||
|     	# Then iterate through each line in arpOutput |  | ||||||
|     	for line in arpOutput: |  | ||||||
|     		line_mac = str(line.split('\t')[1]) |  | ||||||
|  |  | ||||||
|     		# For each line we check after a matching mac addr | #     i = 0 | ||||||
|     		if (mac in line_mac): | #     # Go through each element in list 'addr' | ||||||
|     			logFile[i] = str(users[i]) + ':' + str(time()) + '\n' | #     for mac in addr: | ||||||
|     			# print mac | #     	# Then iterate through each line in arpOutput | ||||||
|     			# print users[i] | #     	for line in arpOutput: | ||||||
|     			# print str(i) + '\n' | #     		line_mac = str(line.split('\t')[1]) | ||||||
|  |  | ||||||
|  | #     		# For each line we check after a matching mac addr | ||||||
|  | #     		if (mac in line_mac): | ||||||
|  | #     			logFile[i] = str(users[i]) + ':' + str(time()) + '\n' | ||||||
|  | #     			# print mac | ||||||
|  | #     			# print users[i] | ||||||
|  | #     			# print str(i) + '\n' | ||||||
|  | #     			# print logFile | ||||||
|  |  | ||||||
|  | #     	i+=1 | ||||||
|  |  | ||||||
|  | #     # Write changes to file | ||||||
|  | #     with open('/home/kevin/homeCheck/times.txt', 'w') as file: | ||||||
|  | #     	file.writelines(logFile) | ||||||
| #     	print logFile | #     	print logFile | ||||||
|  |  | ||||||
|     	i+=1 |  | ||||||
|  |  | ||||||
|     # Write changes to file | # whosHome() | ||||||
|     with open('/home/kevin/homeCheck/times.txt', 'w') as file: |  | ||||||
|     	file.writelines(logFile) |  | ||||||
|     	print logFile |  | ||||||
|  |  | ||||||
|  |  | ||||||
| whosHome() |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user