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
This commit is contained in:
guanana 2020-12-13 04:45:07 +00:00
parent af65c25277
commit 8bfaf2d7d5
5 changed files with 43 additions and 28 deletions

View File

@ -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
))

View File

@ -8,6 +8,7 @@ tls_verify = no
[NMAP]
path = ./
networks = networks.txt
unknown = autodiscovered:netbox-scanner
tag = nmap
cleanup = no

View File

@ -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)

5
networks.txt Normal file
View File

@ -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

View File

@ -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