mirror of
https://github.com/lopes/netbox-scanner.git
synced 2025-07-03 03:54:44 +02:00
using logging, improved documentation
This commit is contained in:
parent
ee065396f4
commit
18f2ff441c
@ -4,9 +4,16 @@ A scanner util for NetBox, because certain networks can be updated automagically
|
|||||||
## Installation
|
## Installation
|
||||||
`netbox-scanner` is available as a Python package via PyPi, so you can install it using `pip`:
|
`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.
|
Note that `netbox-scanner` will require Nmap and an instance of NetBox ready to use.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
`netbox-scanner` can be used both in your programs or as a script to be used in shell.
|
`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.
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
# netbox-scanner configuration file.
|
# netbox-scanner configuration file.
|
||||||
|
|
||||||
|
from logging import DEBUG
|
||||||
|
|
||||||
NETBOX = {
|
NETBOX = {
|
||||||
'ADDRESS': '',
|
'ADDRESS': '',
|
||||||
'TOKEN': '',
|
'TOKEN': '',
|
||||||
@ -7,7 +9,22 @@ NETBOX = {
|
|||||||
'PORT': 443,
|
'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'
|
UNKNOWN_HOSTNAME = 'UNKNOWN HOST'
|
||||||
DISABLE_TLS_WARNINGS = True # stop displaying TLS/SSL warnings?
|
DISABLE_TLS_WARNINGS = True # stop displaying TLS/SSL warnings?
|
||||||
|
|
||||||
|
@ -7,14 +7,14 @@ from nmap import PortScanner
|
|||||||
from cpe import CPE
|
from cpe import CPE
|
||||||
from netbox import NetBox
|
from netbox import NetBox
|
||||||
|
|
||||||
from config import TAGS, UNKNOWN_HOSTNAME
|
|
||||||
|
|
||||||
|
|
||||||
class NetBoxScanner(object):
|
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,
|
self.netbox = NetBox(host=host, use_ssl=tls, auth_token=token,
|
||||||
port=port)
|
port=port)
|
||||||
|
self.tag = tag
|
||||||
|
self.unknown = unknown
|
||||||
if warnings:
|
if warnings:
|
||||||
disable_warnings(InsecureRequestWarning)
|
disable_warnings(InsecureRequestWarning)
|
||||||
|
|
||||||
@ -27,7 +27,12 @@ class NetBoxScanner(object):
|
|||||||
c.get_product()[0], c.get_version()[0])
|
c.get_product()[0], c.get_version()[0])
|
||||||
|
|
||||||
def scan(self, network):
|
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 = []
|
hosts = []
|
||||||
nm = PortScanner()
|
nm = PortScanner()
|
||||||
nm.scan(network, arguments='-T4 -O -F')
|
nm.scan(network, arguments='-T4 -O -F')
|
||||||
@ -38,27 +43,32 @@ class NetBoxScanner(object):
|
|||||||
description = self.get_description(nm[host]['hostnames'][0]['name'],
|
description = self.get_description(nm[host]['hostnames'][0]['name'],
|
||||||
nm[host]['osmatch'][0]['osclass'][0]['cpe'])
|
nm[host]['osmatch'][0]['osclass'][0]['cpe'])
|
||||||
except (KeyError, AttributeError):
|
except (KeyError, AttributeError):
|
||||||
description = UNKNOWN_HOSTNAME
|
description = self.unknown
|
||||||
hosts.append({'address':address,'description':description})
|
hosts.append({'address':address,'description':description})
|
||||||
return hosts
|
return hosts
|
||||||
|
|
||||||
def sync(self, networks):
|
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:
|
for net in networks:
|
||||||
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 (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'])
|
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=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):
|
for ipv4 in IPv4Network(net):
|
||||||
address = str(ipv4)
|
address = str(ipv4)
|
||||||
if not any(h['address'] == address for h in hosts):
|
if not any(h['address'] == address for h in hosts):
|
||||||
nbhost = self.netbox.ipam.get_ip_addresses(address=address)
|
nbhost = self.netbox.ipam.get_ip_addresses(address=address)
|
||||||
try:
|
try:
|
||||||
if TAGS[0] in nbhost[0]['tags']:
|
if self.tag in nbhost[0]['tags']:
|
||||||
self.netbox.ipam.delete_ip_address(address)
|
self.netbox.ipam.delete_ip_address(address)
|
||||||
except IndexError:
|
except IndexError:
|
||||||
pass
|
pass
|
||||||
|
@ -1,12 +1,21 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
from logging import getLogger
|
||||||
|
from logging.config import dictConfig
|
||||||
|
|
||||||
|
import config
|
||||||
from nbscan import NetBoxScanner
|
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(config.NETBOX['ADDRESS'], config.NETBOX['TLS'],
|
||||||
nbs = NetBoxScanner(NETBOX['ADDRESS'], NETBOX['TLS'],
|
config.NETBOX['TOKEN'], config.NETBOX['PORT'], config.TAG,
|
||||||
NETBOX['TOKEN'], NETBOX['PORT'], DISABLE_TLS_WARNINGS)
|
config.UNKNOWN_HOSTNAME, config.DISABLE_TLS_WARNINGS)
|
||||||
nbs.sync(TARGETS)
|
|
||||||
print('finishing - {}'.format(datetime.now()))
|
logger.debug('starting')
|
||||||
|
nbs.sync(config.TARGETS)
|
||||||
|
logger.debug('finished')
|
||||||
|
|
||||||
exit(0)
|
exit(0)
|
||||||
|
1975107045
|
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.0.4",
|
version="0.0.5",
|
||||||
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