NEWS
Was ist in meinen Netzwerk los?
-
Hallo,
ich möchte gerne wissen, was in meinen Netzwerk los ist, d.h. wann kommt ein neues Devices hinzu, wann wird eine neue IP-Adresse generiert etc.
Ich generiere mir bei neuen devices eine Push-Nachricht.Dafür braucht mein ein wenig Skript (hier python):
from getmac import get_mac_address import csv import socket import pandas as pd import threading import datetime import os import requests akt = datetime.datetime.now() def get_vendor(mac): sst = mac[0:8] name = names.get(sst) if name is None: return "? => ?" return name def pushOver(txt): title = "Geraet ! " message = "Neues Geraet im Haus \n" + txt + "\n" + akt.strftime("%Y-%m-%d %H:%M:%S") + " !" r = requests.post("https://api.pushover.net/1/messages.json", data={ "token": "--token--", "user": "--user--", "title": title, "message": message, "priority": 1 }) names = {} with open('oui.csv') as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') for ar in csv_reader: search = ar[1][0:2] + ":" + ar[1][2:4] + ":" + ar[1][4:6] search = search.lower() names[search] = ar[2] + " => " + ar[3] aktip = {} macetc = {} allmac = {} with open('my.csv') as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') for ar in csv_reader: aktip[ar[0]] = ar[1] macetc[ar[1]] = ar with open('mymac.csv') as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') for ar in csv_reader: allmac[ar[0]] = ar class ip_check(threading.Thread): def __init__(self, ip): threading.Thread.__init__(self) self.ip = ip self.__name = "" self.__company = "" self.__mac = "" self.__newdev = True self.__newip = True self.__newmac = True self.__newipmac = True def run(self): self.__mac = get_mac_address(ip=self.ip) if (self.__mac is not None) and (self.__mac != "00:00:00:00:00:00"): self.__company = get_vendor(self.__mac) try: self.__name = socket.gethostbyaddr(self.ip)[0] except: self.__name = "?.local" smac = aktip.get(self.ip) if smac is not None: self.__newdev = False self.__newip = False if smac == self.__mac: self.__newipmac = False line = allmac.get(self.__mac) if line is not None: self.__newmac = False if line[3] == self.ip: self.__newipmac = False self.__newip = False def status(self): if self.found(): return "" + self.ip + " " + self.__mac + " " + self.__name + " " + self.__company else: return "?" def getLanList(self): if self.found(): txt = self.ip + " " + self.__name txt = txt + " " + self.__name.split(".")[0] return txt else: return "" def devStat(self): stat = "old mac+ip"; if self.__newdev: stat = "new " if self.__newmac: stat = "new mac " if self.__newip: stat = "new mac+ip" else: if self.__newip: stat = "new ip" return stat def devOk(self): return self.devStat() == "old mac+ip" def doPush(self): if self.devStat() == "new mac ": return True if self.devStat() == "new mac+ip": return True return False def getmac(self): return self.__mac def getcsv(self): if self.found(): return [self.ip, self.__mac, self.__name, self.__company] else: return [] def getAllMac(self): if self.found(): return [self.__mac, self.__name, self.__company, self.ip] else: return [] def found(self): if (self.__mac is None) or (self.__mac == "00:00:00:00:00:00"): return False else: return True check_results = [] for i in range(1, 255): ip = "192.168.1." + str(i) ipp = "192.168.1." + ("000" + str(i))[-3:] current = ip_check(ip) check_results.append(current) current.start() with open('my.csv', 'w') as csv_file: csv_writer = csv.writer(csv_file, delimiter=',', quoting=csv.QUOTE_ALL) for el in check_results: el.join() if el.found(): allmac[el.getmac()] = el.getAllMac() try: macetc.pop(el.getmac()) except Exception as e: sub = 0 if el.devOk() == False: print(akt.strftime("%Y-%m-%d %H:%M:%S") + ":" + el.devStat() + " " + el.status()) if el.doPush() == True: pushOver(el.status()) csv_writer.writerow(el.getcsv()) # print(el.getLanList()) for el in macetc: if el is not None: val = macetc.get(el) print(akt.strftime("%Y-%m-%d %H:%M:%S") + ":DEL " + " ".join(val) + "<br>") if os.path.exists("mymac.csv"): os.remove("mymac.csv") with open('mymac.csv', 'w') as csv_file: csv_writer = csv.writer(csv_file, delimiter=',', quoting=csv.QUOTE_ALL) for ar in allmac: val = allmac.get(ar) csv_writer.writerow(val) # ende = datetime.datetime.now() # print("Start: " + akt.strftime("%Y-%m-%d %H:%M:%S") + " Dauer:" + str(ende - akt)) df = pd.read_csv('my.csv', names=['IP', 'MAC', 'Name', 'Hersteller']) # Save to file df.to_html('mydev.html')
Hier die DB für MAC->Hersteller:
oui.csvUnd hier ein wenig Shell-Skript, um die Ergebnisse aufzuhübschen:
#!/bin/bash logfile=/var/skripte/startGetNet.log echo "$(date) Starte getNet" >> $logfile cd /var/skripte python3 -u getnet.py >> $logfile echo "<html><head>" > /var/www/html/mydev.html echo "<title>WH:Devices</title>" >> /var/www/html/mydev.html echo "<meta http-equiv='refresh' content='60'>" >> /var/www/html/mydev.html echo "</head><body>" >> /var/www/html/mydev.html echo "<h2>Devices - $(date)</h2>" >> /var/www/html/mydev.html #more mydev.html >> /var/www/html/mydev.html #grep -v getNet $logfile | tail -100 | sort -r >> /var/www/html/mydev.html if [ -f "${logfile}" ] then printf "<h3>NEW Logfile</h3><table border=\"1\">" >> /var/www/html/mydev.html printf "<tr><th>Datum</th><th>Zeit</th><th>OP</th><th>IP</th><th>MAC</th><th>Name</th><th>Hersteller</th></tr>" >> /var/www/html/mydev.html grep "new mac\|new ip" $logfile | tail -10 | sort -r | sed "s/\(^[0-9-]*\) \([0-9:]*\):\([ a-zA-Z+]*\)\([0-9.]*\) \([0-9a-z:]*\) \([.?a-zA-Z0-9_-]*\) /\1<\/td><td>\2<\ /td><td>\3<\/td><td>\4<\/td><td>\5<\/td><td>\6<\/td><td>/g" | while read line do printf "<tr><td>${line}</td></tr>" >> /var/www/html/mydev.html done printf "</table>" >> /var/www/html/mydev.html fi echo "<h3>Liste</h3>" >> /var/www/html/mydev.html more mydev.html >> /var/www/html/mydev.html if [ -f "${logfile}" ] then printf "<h3>CHANGE Logfile</h3><table border=\"1\">" >> /var/www/html/mydev.html printf "<tr><th>Datum</th><th>Zeit</th><th>OP</th><th>IP</th><th>MAC</th><th>Name</th><th>Hersteller</th></tr>" >> /var/www/html/mydev.html grep -v getNet $logfile | tail -100 | sort -r | sed "s/\(^[0-9-]*\) \([0-9:]*\):\([ a-zA-Z+]*\)\([0-9.]*\) \([0-9a-z:]*\) \([.?a-zA-Z0-9_-]*\) /\1<\/td><td>\2<\/td><td>\3< \/td><td>\4<\/td><td>\5<\/td><td>\6<\/td><td>/g" | while read line do printf "<tr><td>${line}</td></tr>" >> /var/www/html/mydev.html done printf "</table>" >> /var/www/html/mydev.html fi echo "</body></html>" >> /var/www/html/mydev.html
Was ist anzupassen:
Verzeichnisse in Shell-Skipt
Token für pushover
Die python Datei und das oui,csv liegen im gleichen Verzeichnis.Ist mit python 3.7 erstellt worden.