mirror of
https://github.com/lopes/netbox-scanner.git
synced 2025-07-21 12:54:39 +02:00
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:
parent
af65c25277
commit
8bfaf2d7d5
47
nbs/nmap.py
47
nbs/nmap.py
@ -1,30 +1,33 @@
|
|||||||
import os
|
import nmap3
|
||||||
import xml.etree.ElementTree as ET
|
|
||||||
|
|
||||||
|
|
||||||
class Nmap(object):
|
class Nmap(object):
|
||||||
|
|
||||||
def __init__(self, path, unknown):
|
def __init__(self, unknown, networks):
|
||||||
self.unknown = unknown
|
self.unknown = unknown
|
||||||
self.path = path
|
self.networks = networks
|
||||||
self.hosts = list()
|
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):
|
def run(self):
|
||||||
for f in os.listdir(self.path):
|
scan_result = self.scan()
|
||||||
if not f.endswith('.xml'):
|
scan_result.pop("stats")
|
||||||
continue
|
scan_result.pop("runtime")
|
||||||
abspath = os.path.join(self.path, f)
|
for k,v in scan_result.items():
|
||||||
tree = ET.parse(abspath)
|
try:
|
||||||
root = tree.getroot()
|
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
|
|
||||||
))
|
|
||||||
|
@ -8,6 +8,7 @@ tls_verify = no
|
|||||||
|
|
||||||
[NMAP]
|
[NMAP]
|
||||||
path = ./
|
path = ./
|
||||||
|
networks = networks.txt
|
||||||
unknown = autodiscovered:netbox-scanner
|
unknown = autodiscovered:netbox-scanner
|
||||||
tag = nmap
|
tag = nmap
|
||||||
cleanup = no
|
cleanup = no
|
||||||
|
@ -24,12 +24,15 @@ if argument == 'prime':
|
|||||||
|
|
||||||
local_config = expanduser('~/.netbox-scanner.conf')
|
local_config = expanduser('~/.netbox-scanner.conf')
|
||||||
global_config = '/opt/netbox/netbox-scanner.conf'
|
global_config = '/opt/netbox/netbox-scanner.conf'
|
||||||
|
dir_config = './netbox-scanner.conf'
|
||||||
config = ConfigParser()
|
config = ConfigParser()
|
||||||
|
|
||||||
if isfile(local_config):
|
if isfile(local_config):
|
||||||
config.read(local_config)
|
config.read(local_config)
|
||||||
elif isfile(global_config):
|
elif isfile(global_config):
|
||||||
config.read(global_config)
|
config.read(global_config)
|
||||||
|
elif isfile(dir_config):
|
||||||
|
config.read(dir_config)
|
||||||
else:
|
else:
|
||||||
raise FileNotFoundError('Configuration file was not found.')
|
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
|
# useful if you have tls_verify set to no
|
||||||
disable_warnings(InsecureRequestWarning)
|
disable_warnings(InsecureRequestWarning)
|
||||||
|
|
||||||
|
with open(nmap['networks'], 'r') as file:
|
||||||
|
networks = file.readlines()
|
||||||
|
|
||||||
def cmd_nmap(s): # nmap handler
|
def cmd_nmap(s): # nmap handler
|
||||||
h = Nmap(nmap['path'], nmap['unknown'])
|
h = Nmap(nmap['unknown'], networks)
|
||||||
h.run()
|
h.run()
|
||||||
s.sync(h.hosts)
|
s.sync(h.hosts)
|
||||||
|
|
||||||
|
5
networks.txt
Normal file
5
networks.txt
Normal 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
|
@ -1,7 +1,8 @@
|
|||||||
certifi==2020.4.5.1
|
certifi==2020.12.5
|
||||||
chardet==3.0.4
|
chardet==3.0.4
|
||||||
idna==2.9
|
idna==2.10
|
||||||
pynetbox==4.3.1
|
pynetbox==5.1.0
|
||||||
requests==2.23.0
|
requests==2.25.0
|
||||||
six==1.15.0
|
six==1.15.0
|
||||||
urllib3==1.25.9
|
urllib3==1.26.2
|
||||||
|
python3-nmap==1.4.9
|
||||||
|
Loading…
x
Reference in New Issue
Block a user