From 8bfaf2d7d5d9ee54a01855cc3d09b654972a84a7 Mon Sep 17 00:00:00 2001 From: guanana Date: Sun, 13 Dec 2020 04:45:07 +0000 Subject: [PATCH] netbox-scanner.conf: Add networks path netbox-scanner.py: Add dir_config option Read networks file networks.txt: Move to expected location and added some examples nmap.py: Use of nmap library to simplify nmap scan requirements.txt: Update requirements.txt with nmap library and update other libraries to latest compatible version --- nbs/nmap.py | 47 ++++++++++++++++++++++++--------------------- netbox-scanner.conf | 1 + netbox-scanner.py | 7 ++++++- networks.txt | 5 +++++ requirements.txt | 11 ++++++----- 5 files changed, 43 insertions(+), 28 deletions(-) create mode 100644 networks.txt diff --git a/nbs/nmap.py b/nbs/nmap.py index e1615bb..168a77b 100644 --- a/nbs/nmap.py +++ b/nbs/nmap.py @@ -1,30 +1,33 @@ -import os -import xml.etree.ElementTree as ET - +import nmap3 class Nmap(object): - def __init__(self, path, unknown): + def __init__(self, unknown, networks): self.unknown = unknown - self.path = path + self.networks = networks self.hosts = list() + self.scan_results = {} + + def scan(self): + nmap = nmap3.NmapHostDiscovery() # instantiate nmap object + for item in self.networks: + temp_scan_result = nmap.nmap_no_portscan(item.replace('\n', '')) + self.scan_results = {**self.scan_results, **temp_scan_result} + return self.scan_results def run(self): - for f in os.listdir(self.path): - if not f.endswith('.xml'): - continue - abspath = os.path.join(self.path, f) - tree = ET.parse(abspath) - root = tree.getroot() + scan_result = self.scan() + scan_result.pop("stats") + scan_result.pop("runtime") + for k,v in scan_result.items(): + try: + self.hosts.append(( + k, + v['hostname'][0]['name'] + )) + except (IndexError, KeyError): + self.hosts.append(( + k, + self.unknown + )) - for host in root.findall('host'): - try: - self.hosts.append(( - host.find('address').attrib['addr'], - host.find('hostnames').find('hostname').attrib['name'] - )) - except AttributeError: - self.hosts.append(( - host.find('address').attrib['addr'], - self.unknown - )) diff --git a/netbox-scanner.conf b/netbox-scanner.conf index e36825f..820693f 100644 --- a/netbox-scanner.conf +++ b/netbox-scanner.conf @@ -8,6 +8,7 @@ tls_verify = no [NMAP] path = ./ +networks = networks.txt unknown = autodiscovered:netbox-scanner tag = nmap cleanup = no diff --git a/netbox-scanner.py b/netbox-scanner.py index 1815c07..8134e71 100644 --- a/netbox-scanner.py +++ b/netbox-scanner.py @@ -24,12 +24,15 @@ if argument == 'prime': local_config = expanduser('~/.netbox-scanner.conf') global_config = '/opt/netbox/netbox-scanner.conf' +dir_config = './netbox-scanner.conf' config = ConfigParser() if isfile(local_config): config.read(local_config) elif isfile(global_config): config.read(global_config) +elif isfile(dir_config): + config.read(dir_config) else: raise FileNotFoundError('Configuration file was not found.') @@ -66,9 +69,11 @@ logging.getLogger().addHandler(logging.StreamHandler()) # useful if you have tls_verify set to no disable_warnings(InsecureRequestWarning) +with open(nmap['networks'], 'r') as file: + networks = file.readlines() def cmd_nmap(s): # nmap handler - h = Nmap(nmap['path'], nmap['unknown']) + h = Nmap(nmap['unknown'], networks) h.run() s.sync(h.hosts) diff --git a/networks.txt b/networks.txt new file mode 100644 index 0000000..b40af15 --- /dev/null +++ b/networks.txt @@ -0,0 +1,5 @@ +192.168.2.0/24 +192.168.3.0/24 +192.168.4.0/24 +192.168.5.0/24 +192.168.15.0/24 diff --git a/requirements.txt b/requirements.txt index c4120ab..f5dd287 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,8 @@ -certifi==2020.4.5.1 +certifi==2020.12.5 chardet==3.0.4 -idna==2.9 -pynetbox==4.3.1 -requests==2.23.0 +idna==2.10 +pynetbox==5.1.0 +requests==2.25.0 six==1.15.0 -urllib3==1.25.9 +urllib3==1.26.2 +python3-nmap==1.4.9