From 18f2ff441cf7e365e2489c1d71a85f0709b2c68f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Lopes?= Date: Wed, 19 Sep 2018 16:45:35 -0300 Subject: [PATCH] using logging, improved documentation --- README.md | 9 ++++++++- netbox-scanner/config.py | 19 ++++++++++++++++++- netbox-scanner/nbscan.py | 26 ++++++++++++++++++-------- netbox-scanner/netbox-scanner.py | 23 ++++++++++++++++------- setup.py | 2 +- 5 files changed, 61 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 1b26343..fd69926 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,16 @@ A scanner util for NetBox, because certain networks can be updated automagically ## Installation `netbox-scanner` is available as a Python package via PyPi, so you can install it using `pip`: - pip3 install netbox-scanner + $ pip3 install netbox-scanner Note that `netbox-scanner` will require Nmap and an instance of NetBox ready to use. ## Usage `netbox-scanner` can be used both in your programs or as a script to be used in shell. + +To use `netbox-scanner` as a script, edit `netbox-scanner/config.py` with your setup, and run the command below: + + $ netbox-scanner.py + +## License +`netbox-scanner` is licensed under a MIT license --read `LICENSE` file for more information. diff --git a/netbox-scanner/config.py b/netbox-scanner/config.py index 245144d..e61b624 100644 --- a/netbox-scanner/config.py +++ b/netbox-scanner/config.py @@ -1,5 +1,7 @@ # netbox-scanner configuration file. +from logging import DEBUG + NETBOX = { 'ADDRESS': '', 'TOKEN': '', @@ -7,7 +9,22 @@ NETBOX = { 'PORT': 443, } -TAGS = ['auto'] # only 1 tag is allowed +LOGGING_CONFIG = dict( + version = 1, + formatters = { + 'f': {'format': '%(asctime)s %(name)-12s %(levelname)-8s %(message)s'} + }, + handlers = { + 'h': { + 'class': 'logging.StreamHandler', + 'formatter': 'f', + 'level': DEBUG + } + }, + root = {'handlers': ['h'], 'level': DEBUG}, +) + +TAG = 'auto' UNKNOWN_HOSTNAME = 'UNKNOWN HOST' DISABLE_TLS_WARNINGS = True # stop displaying TLS/SSL warnings? diff --git a/netbox-scanner/nbscan.py b/netbox-scanner/nbscan.py index f59dc72..20c49e8 100644 --- a/netbox-scanner/nbscan.py +++ b/netbox-scanner/nbscan.py @@ -7,14 +7,14 @@ from nmap import PortScanner from cpe import CPE from netbox import NetBox -from config import TAGS, UNKNOWN_HOSTNAME - class NetBoxScanner(object): - def __init__(self, host, tls, token, port, warnings=True): + def __init__(self, host, tls, token, port, tag, unknown, warnings=True): self.netbox = NetBox(host=host, use_ssl=tls, auth_token=token, port=port) + self.tag = tag + self.unknown = unknown if warnings: disable_warnings(InsecureRequestWarning) @@ -27,7 +27,12 @@ class NetBoxScanner(object): c.get_product()[0], c.get_version()[0]) def scan(self, network): - '''''' + '''Scan a network. + + :param network: a valid network, like 10.0.0.0/8 + :return: a list with dictionaries of responsive + hosts (addr and description) + ''' hosts = [] nm = PortScanner() nm.scan(network, arguments='-T4 -O -F') @@ -38,27 +43,32 @@ class NetBoxScanner(object): description = self.get_description(nm[host]['hostnames'][0]['name'], nm[host]['osmatch'][0]['osclass'][0]['cpe']) except (KeyError, AttributeError): - description = UNKNOWN_HOSTNAME + description = self.unknown hosts.append({'address':address,'description':description}) return hosts def sync(self, networks): + '''Scan some networks and sync them to NetBox. + + :param networks: a list of valid networks, like ['10.0.0.0/8'] + :return: nothing will be returned + ''' for net in networks: hosts = self.scan(net) for host in hosts: nbhost = self.netbox.ipam.get_ip_addresses(address=host['address']) if nbhost: - if (TAGS[0] in nbhost[0]['tags']) and (host['description'] != nbhost[0]['description']): + 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']) else: - self.netbox.ipam.create_ip_address('{}/32'.format(host['address']), tags=TAGS, description=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) if not any(h['address'] == address for h in hosts): nbhost = self.netbox.ipam.get_ip_addresses(address=address) try: - if TAGS[0] in nbhost[0]['tags']: + if self.tag in nbhost[0]['tags']: 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 0d96b49..d276beb 100644 --- a/netbox-scanner/netbox-scanner.py +++ b/netbox-scanner/netbox-scanner.py @@ -1,12 +1,21 @@ +#!/usr/bin/env python3 + +from logging import getLogger +from logging.config import dictConfig + +import config from nbscan import NetBoxScanner -from config import NETBOX, DISABLE_TLS_WARNINGS, TARGETS -from datetime import datetime +dictConfig(config.LOGGING_CONFIG) +logger = getLogger('netbox-scanner') -print('starting - {}'.format(datetime.now())) -nbs = NetBoxScanner(NETBOX['ADDRESS'], NETBOX['TLS'], - NETBOX['TOKEN'], NETBOX['PORT'], DISABLE_TLS_WARNINGS) -nbs.sync(TARGETS) -print('finishing - {}'.format(datetime.now())) +nbs = NetBoxScanner(config.NETBOX['ADDRESS'], config.NETBOX['TLS'], + config.NETBOX['TOKEN'], config.NETBOX['PORT'], config.TAG, + config.UNKNOWN_HOSTNAME, config.DISABLE_TLS_WARNINGS) + +logger.debug('starting') +nbs.sync(config.TARGETS) +logger.debug('finished') exit(0) +1975107045 \ No newline at end of file diff --git a/setup.py b/setup.py index bfc403e..37037e2 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.0.4", + version="0.0.5", author='José Lopes de Oliveira Jr.', author_email="jlojunior@gmail.com", description="A scanner util for NetBox",