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:
|
`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:
|
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.
|
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.
|
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`.
|
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`.
|
Of course, you can use `cron` to automatically run `netbox-scanner`.
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
@ -11,6 +11,6 @@ TAG = 'auto'
|
|||||||
UNKNOWN_HOSTNAME = 'UNKNOWN HOST'
|
UNKNOWN_HOSTNAME = 'UNKNOWN HOST'
|
||||||
DISABLE_TLS_WARNINGS = True # stop displaying TLS/SSL warnings?
|
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']
|
# 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 import disable_warnings
|
||||||
from urllib3.exceptions import InsecureRequestWarning
|
from urllib3.exceptions import InsecureRequestWarning
|
||||||
from ipaddress import IPv4Network
|
from ipaddress import IPv4Network
|
||||||
@ -54,14 +54,23 @@ class NetBoxScanner(object):
|
|||||||
:return: nothing will be returned
|
:return: nothing will be returned
|
||||||
'''
|
'''
|
||||||
for net in networks:
|
for net in networks:
|
||||||
|
logging.info('scanning network {}'.format(net))
|
||||||
hosts = self.scan(net)
|
hosts = self.scan(net)
|
||||||
for host in hosts:
|
for host in hosts:
|
||||||
nbhost = self.netbox.ipam.get_ip_addresses(address=host['address'])
|
nbhost = self.netbox.ipam.get_ip_addresses(address=host['address'])
|
||||||
if nbhost:
|
if nbhost:
|
||||||
if (self.tag in nbhost[0]['tags']) and (host['description'] != nbhost[0]['description']):
|
if (self.tag in nbhost[0]['tags']) and (host['description'] !=
|
||||||
self.netbox.ipam.update_ip('{}/32'.format(host['address']), description=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:
|
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):
|
for ipv4 in IPv4Network(net):
|
||||||
address = str(ipv4)
|
address = str(ipv4)
|
||||||
@ -69,6 +78,8 @@ class NetBoxScanner(object):
|
|||||||
nbhost = self.netbox.ipam.get_ip_addresses(address=address)
|
nbhost = self.netbox.ipam.get_ip_addresses(address=address)
|
||||||
try:
|
try:
|
||||||
if self.tag in nbhost[0]['tags']:
|
if self.tag in nbhost[0]['tags']:
|
||||||
|
logging.warning('deleting host {} ({})'.format(
|
||||||
|
host['address'], host['description']))
|
||||||
self.netbox.ipam.delete_ip_address(address)
|
self.netbox.ipam.delete_ip_address(address)
|
||||||
except IndexError:
|
except IndexError:
|
||||||
pass
|
pass
|
||||||
|
@ -1,26 +1,38 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import logging.handlers as handlers
|
from sys import stdout, stderr
|
||||||
|
from argparse import ArgumentParser
|
||||||
|
|
||||||
import config
|
import config
|
||||||
from nbscan import NetBoxScanner
|
from nbscan import NetBoxScanner
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger('netbox-scanner')
|
logging.basicConfig(filename='netbox-scanner.log', level=logging.INFO,
|
||||||
logger.setLevel(logging.INFO)
|
format='%(asctime)s\t%(name)s\t%(levelname)s\t%(message)s')
|
||||||
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)
|
|
||||||
|
|
||||||
nbs = NetBoxScanner(config.NETBOX['ADDRESS'], config.NETBOX['TLS'],
|
argp = ArgumentParser()
|
||||||
config.NETBOX['TOKEN'], config.NETBOX['PORT'], config.TAG,
|
argp.add_argument('-a', '--address', help='netbox address',
|
||||||
config.UNKNOWN_HOSTNAME, config.DISABLE_TLS_WARNINGS)
|
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 = NetBoxScanner(args.address, args.tls, args.token, args.port,
|
||||||
nbs.sync(config.TARGETS)
|
args.tag, args.unknown, args.warnings)
|
||||||
logger.info('finished')
|
|
||||||
|
nbs.sync(args.networks)
|
||||||
|
|
||||||
exit(0)
|
exit(0)
|
||||||
|
2
setup.py
2
setup.py
@ -7,7 +7,7 @@ with open("README.md", "r") as fh:
|
|||||||
|
|
||||||
setuptools.setup(
|
setuptools.setup(
|
||||||
name="netbox-scanner",
|
name="netbox-scanner",
|
||||||
version="0.1.1",
|
version="0.2.0",
|
||||||
author='José Lopes de Oliveira Jr.',
|
author='José Lopes de Oliveira Jr.',
|
||||||
author_email="jlojunior@gmail.com",
|
author_email="jlojunior@gmail.com",
|
||||||
description="A scanner util for NetBox",
|
description="A scanner util for NetBox",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user