From 5b51e83468a39baf0dde01ebbba531d5347fce17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Lopes?= Date: Fri, 21 Sep 2018 12:50:36 -0300 Subject: [PATCH] improved logging, cli added --- README.md | 6 ++++- netbox-scanner/config.py | 4 ++-- netbox-scanner/nbscan.py | 19 +++++++++++---- netbox-scanner/netbox-scanner.py | 40 +++++++++++++++++++++----------- setup.py | 2 +- 5 files changed, 49 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 2c07853..a19c039 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/netbox-scanner/config.py b/netbox-scanner/config.py index 3cae2e7..7cdd9ee 100644 --- a/netbox-scanner/config.py +++ b/netbox-scanner/config.py @@ -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 = [] diff --git a/netbox-scanner/nbscan.py b/netbox-scanner/nbscan.py index ab84cd2..737409b 100644 --- a/netbox-scanner/nbscan.py +++ b/netbox-scanner/nbscan.py @@ -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 diff --git a/netbox-scanner/netbox-scanner.py b/netbox-scanner/netbox-scanner.py index 94fe925..284e681 100644 --- a/netbox-scanner/netbox-scanner.py +++ b/netbox-scanner/netbox-scanner.py @@ -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) diff --git a/setup.py b/setup.py index dd7300a..e61c57c 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ with open("README.md", "r") as fh: setuptools.setup( name="netbox-scanner", - version="0.1.1", + version="0.2.0", author='José Lopes de Oliveira Jr.', author_email="jlojunior@gmail.com", description="A scanner util for NetBox",