mirror of
https://github.com/lopes/netbox-scanner.git
synced 2025-07-02 19:44:38 +02:00
improved logging, cli added
This commit is contained in:
parent
d41ba6fce7
commit
5b51e83468
@ -24,7 +24,7 @@ Note that `netbox-scanner` will require Nmap and an instance of NetBox ready to
|
||||
|
||||
`netbox-scanner` will do the following tasks:
|
||||
|
||||
1. It will scan all networks defined in `netbox-scanner/config.py`.
|
||||
1. It will scan all networks defined in `netbox-scanner/config.py` or via parameters.
|
||||
2. For each discovered host it will:
|
||||
1. If host is in NetBox, description is different, and tag is set as defined in `netbox-scanner/config.py/TAG`, it'll be updated.
|
||||
2. If host is not in NetBox, it'll be created.
|
||||
@ -32,6 +32,10 @@ Note that `netbox-scanner` will require Nmap and an instance of NetBox ready to
|
||||
|
||||
This way, if some hosts in your networks that are monitored via `netbox-scanner` are eventually down, but you don't want to delete them, just make sure that it doesn't have the tag as set in `netbox-scanner/config.py/TAG`.
|
||||
|
||||
To see a list of all available parameters in `netbox-scanner.py`, simple use the `-h` option --please note that all parameters are optional, because all of them can be set using `netbox-scanner/config.py` file:
|
||||
|
||||
$ netbox-scanner.py -h
|
||||
|
||||
Of course, you can use `cron` to automatically run `netbox-scanner`.
|
||||
|
||||
## License
|
||||
|
@ -11,6 +11,6 @@ TAG = 'auto'
|
||||
UNKNOWN_HOSTNAME = 'UNKNOWN HOST'
|
||||
DISABLE_TLS_WARNINGS = True # stop displaying TLS/SSL warnings?
|
||||
|
||||
# These are the targets to be scanned.
|
||||
# These are the networks to be scanned.
|
||||
# Example: ['192.168.40.0/20', '10.2.50.0/24']
|
||||
TARGETS = []
|
||||
NETWORKS = []
|
||||
|
@ -1,4 +1,4 @@
|
||||
|
||||
import logging
|
||||
from urllib3 import disable_warnings
|
||||
from urllib3.exceptions import InsecureRequestWarning
|
||||
from ipaddress import IPv4Network
|
||||
@ -54,14 +54,23 @@ class NetBoxScanner(object):
|
||||
:return: nothing will be returned
|
||||
'''
|
||||
for net in networks:
|
||||
logging.info('scanning network {}'.format(net))
|
||||
hosts = self.scan(net)
|
||||
for host in hosts:
|
||||
nbhost = self.netbox.ipam.get_ip_addresses(address=host['address'])
|
||||
if nbhost:
|
||||
if (self.tag in nbhost[0]['tags']) and (host['description'] != nbhost[0]['description']):
|
||||
self.netbox.ipam.update_ip('{}/32'.format(host['address']), description=host['description'])
|
||||
if (self.tag in nbhost[0]['tags']) and (host['description'] !=
|
||||
nbhost[0]['description']):
|
||||
logging.warning('updating host {} ({}) to: {}'.format(
|
||||
host['address'], nbhost[0]['description'],
|
||||
host['description']))
|
||||
self.netbox.ipam.update_ip('{}/32'.format(host['address']),
|
||||
description=host['description'])
|
||||
else:
|
||||
self.netbox.ipam.create_ip_address('{}/32'.format(host['address']), tags=[self.tag], description=host['description'])
|
||||
logging.info('creating host {} ({})'.format(host['address'],
|
||||
host['description']))
|
||||
self.netbox.ipam.create_ip_address('{}/32'.format(host['address']),
|
||||
tags=[self.tag], description=host['description'])
|
||||
|
||||
for ipv4 in IPv4Network(net):
|
||||
address = str(ipv4)
|
||||
@ -69,6 +78,8 @@ class NetBoxScanner(object):
|
||||
nbhost = self.netbox.ipam.get_ip_addresses(address=address)
|
||||
try:
|
||||
if self.tag in nbhost[0]['tags']:
|
||||
logging.warning('deleting host {} ({})'.format(
|
||||
host['address'], host['description']))
|
||||
self.netbox.ipam.delete_ip_address(address)
|
||||
except IndexError:
|
||||
pass
|
||||
|
@ -1,26 +1,38 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import logging
|
||||
import logging.handlers as handlers
|
||||
from sys import stdout, stderr
|
||||
from argparse import ArgumentParser
|
||||
|
||||
import config
|
||||
from nbscan import NetBoxScanner
|
||||
|
||||
|
||||
logger = logging.getLogger('netbox-scanner')
|
||||
logger.setLevel(logging.INFO)
|
||||
formatter = logging.Formatter('%(asctime)s\t%(name)s\t%(levelname)s\t%(message)s')
|
||||
loghandler = handlers.TimedRotatingFileHandler('netbox-scanner.log', when='M', interval=1, backupCount=2)
|
||||
loghandler.setLevel(logging.INFO)
|
||||
loghandler.setFormatter(formatter)
|
||||
logger.addHandler(loghandler)
|
||||
logging.basicConfig(filename='netbox-scanner.log', level=logging.INFO,
|
||||
format='%(asctime)s\t%(name)s\t%(levelname)s\t%(message)s')
|
||||
|
||||
nbs = NetBoxScanner(config.NETBOX['ADDRESS'], config.NETBOX['TLS'],
|
||||
config.NETBOX['TOKEN'], config.NETBOX['PORT'], config.TAG,
|
||||
config.UNKNOWN_HOSTNAME, config.DISABLE_TLS_WARNINGS)
|
||||
argp = ArgumentParser()
|
||||
argp.add_argument('-a', '--address', help='netbox address',
|
||||
default=config.NETBOX['ADDRESS'])
|
||||
argp.add_argument('-s', '--tls', help='netbox use tls',
|
||||
action='store_true', default=config.NETBOX['TLS'])
|
||||
argp.add_argument('-t', '--token', help='netbox access token',
|
||||
default=config.NETBOX['TOKEN'])
|
||||
argp.add_argument('-p', '--port', help='netbox access port',
|
||||
default=config.NETBOX['PORT'])
|
||||
argp.add_argument('-g', '--tag', help='netbox-scanner tag',
|
||||
default=config.TAG)
|
||||
argp.add_argument('-u', '--unknown', help='netbox-scanner unknown host',
|
||||
default=config.UNKNOWN_HOSTNAME)
|
||||
argp.add_argument('-w', '--warnings', help='disable tls warnings',
|
||||
action='store_true', default=config.DISABLE_TLS_WARNINGS)
|
||||
argp.add_argument('-n', '--networks', nargs='+', help='networks to be scanned',
|
||||
default=config.NETWORKS)
|
||||
args = argp.parse_args()
|
||||
|
||||
logger.info('starting')
|
||||
nbs.sync(config.TARGETS)
|
||||
logger.info('finished')
|
||||
nbs = NetBoxScanner(args.address, args.tls, args.token, args.port,
|
||||
args.tag, args.unknown, args.warnings)
|
||||
|
||||
nbs.sync(args.networks)
|
||||
|
||||
exit(0)
|
||||
|
Loading…
x
Reference in New Issue
Block a user