-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker.py
executable file
·51 lines (40 loc) · 1.87 KB
/
checker.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
import re
class checker:
def get_ips(self,thefile): #read all the IP from thefile parameter. Return a list containing the IPs
logfile = list(open(str(thefile), 'r').read().split('\n'))
newip = []
for entry in logfile:
ips = re.findall(r'[0-9]+(?:\.[0-9]+){3}', entry)
for ip in ips:
newip.append(ip)
return newip
def read_file(self,thefile): #Return a dictionary after read IP and hostname form a file and store them in the mentioned dictionary with key: IP and value: hostname.
with open(str(thefile),'r') as f: #format file: "IP<space>hostname"
auxlist = []
ip_not_found = True
for line in f:
ip_not_found = False
auxlist.append(line.rstrip())
dictionary = {}
for i in range(len(auxlist)):
auxlist[i] = auxlist[i].split(" ")
dictionary = dict(auxlist)
return dictionary
def get_hostname(self, ip,thefile): #given a IP, return the hostname
data = self.read_file(thefile)
return data.get(ip)
def get_temperature(self):
with open("temperature.txt", "r") as f: #verify path
value = f.read()
return value
def get_hosts(self):
hosts_list = []
keylist = self.get_ips("hosts.txt") #get IPs from hosts.txt (more details about it on: https://github.com/MAInformatico/Raspberry-Pi-Monitoring-Network/tree/master/RaspberryPiFiles )
for i in range(len(keylist)):
hosts_list.append(self.get_hostname(keylist[i],"dictionary.txt")) #where dictionary.txt is the file that contains my "DNS file" Please, create your own file dictionary.txt
if "None" in hosts_list:
print("Unknown device")
return "There is an unknown devices!!"
#print(hostsList)
else:
return hosts_list