diff --git a/fetchers.py b/fetchers.py index 469a04f..cca6ed2 100644 --- a/fetchers.py +++ b/fetchers.py @@ -5,6 +5,7 @@ import datetime as datetimesrc import psutil import subprocess from colors import Colors, getColorsDict +import ipaddress def pretty_mac(_mac): try: @@ -113,7 +114,20 @@ def getMemPercent(colors = True): def getRowSeparator(): return Colors.NEGATIVE + "".join([" " for i in range(0, 60)]) + Colors.END -def getArp(): +def checkIpInPool(ip, pool): + if not ip or not pool: + return True + return ipaddress.ip_address(ip) in ipaddress.ip_network(pool) + +def pingHostViaNMap(ip_pool = None): + if ip_pool is None: + return "" + subprocess.check_output(f"nmap -sn {ip_pool}", shell=True, text=True) + return getDateTime() + +def getArp(ip_pool = None): + #pingHostViaNMap(ip_pool) + global maclookup if maclookup is None: maclookup = MacLookup() @@ -128,6 +142,18 @@ def getArp(): for line in arp_res.split("\n"): if line: host, ip, at, mac, *other = line.split(" ") + ip = ip[1:-1] + if checkIpInPool(ip, ip_pool): + pass + else: + continue + + if mac == "(incomplete)": + continue + + if mac == "FF:FF:FF:FF:FF:FF".lower(): + continue + mac = pretty_mac(mac) if mac not in firstMacLookup: @@ -137,7 +163,7 @@ def getArp(): delta = int(time.time()) - firstMacLookup[mac] result.append({ "host": host, - "ip": ip[1:-1], + "ip": ip, "mac": mac, "mac_name": maclookup.lookup(mac), "delta": delta, diff --git a/service.py b/service.py index 2afaa87..86291ef 100644 --- a/service.py +++ b/service.py @@ -21,13 +21,13 @@ def printColors(): res += f"{v}{k}" print(res) -def createDaemon(func, key, sleep = 60): +def createDaemon(func, key, sleep = 60, *args): from threading import Thread results[key] = "" def resTo(): while True: try: - results[key] = func() + results[key] = func(*args) if logging: print(f'[{time.time()}] Save result to {key}') except: @@ -50,7 +50,8 @@ class Service: createDaemon(getDateTime, "date_time", 1) createDaemon(getCpuPercent, "cpu_percent", 1) createDaemon(getMemPercent, "mem_percent", 1) - createDaemon(getArp, "arp_result", 60) + createDaemon(getArp, "arp_result", 60, self.args.ip_pool) + createDaemon(pingHostViaNMap, "nmap_pinger", 60, self.args.ip_pool) try: from haSensors import getHaSensors createDaemon(getHaSensors, "ha_sensors", 60) @@ -86,6 +87,7 @@ if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--cols", default=60) parser.add_argument("--rows", default=40) + parser.add_argument("--ip-pool", default="192.168.3.0/24") args = parser.parse_args() Service(args) #printColors() \ No newline at end of file