mirror of
https://github.com/lopes/netbox-scanner.git
synced 2025-07-24 06:14:42 +02:00
minor code improvements
This commit is contained in:
parent
1de0ce0340
commit
ff062e72e7
@ -143,6 +143,32 @@ class NetBoxScanner(object):
|
||||
tags=[self.tag], description=host[1])
|
||||
self.logger('created', address=host[0], description=host[1])
|
||||
return True
|
||||
|
||||
def sync_network(self, network):
|
||||
'''Syncs a single network to NetBox.
|
||||
|
||||
:param network: a network with CIDR like '10.0.0.1/24'
|
||||
:return: True if syncing is ok or False in other case.
|
||||
'''
|
||||
hosts = self.scan(network)
|
||||
self.logger('scanned', net=network, hosts=len(hosts))
|
||||
for host in hosts:
|
||||
self.sync_host(host)
|
||||
for ipv4 in IPv4Network(network): # cleanup
|
||||
address = str(ipv4)
|
||||
if not any(h[0]==address for h in hosts):
|
||||
try:
|
||||
nbhost = self.netbox.ipam.ip_addresses.get(address=address)
|
||||
if self.tag in nbhost.tags:
|
||||
nbhost.delete()
|
||||
self.logger('deleted', address=nbhost.address,
|
||||
description=nbhost.description)
|
||||
else:
|
||||
self.logger('undiscovered', address=nbhost.address,
|
||||
description=nbhost.description)
|
||||
except (AttributeError, ValueError):
|
||||
pass
|
||||
return True
|
||||
|
||||
def sync(self, networks):
|
||||
'''Scan some networks and sync them to NetBox.
|
||||
@ -152,29 +178,15 @@ class NetBoxScanner(object):
|
||||
'''
|
||||
for s in self.stats:
|
||||
self.stats[s] = 0
|
||||
|
||||
parsing = self.parser(networks)
|
||||
if parsing:
|
||||
self.logger('mistyped', badnets=parsing)
|
||||
return False
|
||||
|
||||
for net in networks:
|
||||
hosts = self.scan(net)
|
||||
self.logger('scanned', net=net, hosts=len(hosts))
|
||||
for host in hosts:
|
||||
self.sync_host(host)
|
||||
for ipv4 in IPv4Network(net): # cleanup
|
||||
address = str(ipv4)
|
||||
if not any(h[0]==address for h in hosts):
|
||||
try:
|
||||
nbhost = self.netbox.ipam.ip_addresses.get(address=address)
|
||||
if self.tag in nbhost.tags:
|
||||
nbhost.delete()
|
||||
self.logger('deleted', address=nbhost.address,
|
||||
description=nbhost.description)
|
||||
else:
|
||||
self.logger('undiscovered', address=nbhost.address,
|
||||
description=nbhost.description)
|
||||
except (AttributeError, ValueError):
|
||||
pass
|
||||
|
||||
logging.info('started: {} networks'.format(len(networks)))
|
||||
for network in networks:
|
||||
self.sync_network(network)
|
||||
logging.info('finished: +{} ~{} -{} ?{} !{}'.format(
|
||||
self.stats['created'], self.stats['updated'], self.stats['deleted'],
|
||||
self.stats['undiscovered'], self.stats['duplicated']))
|
||||
return True
|
||||
|
@ -5,7 +5,6 @@ import logging
|
||||
from configparser import ConfigParser
|
||||
from os import fsync
|
||||
from os.path import expanduser
|
||||
from getpass import getpass
|
||||
from datetime import datetime
|
||||
from urllib3 import disable_warnings
|
||||
from urllib3.exceptions import InsecureRequestWarning
|
||||
@ -59,7 +58,6 @@ logfile = '{}/netbox-scanner-{}.log'.format(general_conf['log'],
|
||||
datetime.now().strftime('%Y%m%dT%H%M%SZ'))
|
||||
logging.basicConfig(filename=logfile, level=logging.INFO,
|
||||
format='%(asctime)s\tnetbox-scanner\t%(levelname)s\t%(message)s')
|
||||
|
||||
disable_warnings(InsecureRequestWarning)
|
||||
|
||||
|
||||
@ -67,10 +65,5 @@ if __name__ == '__main__':
|
||||
nbs = NetBoxScanner(netbox_conf['address'], netbox_conf['token'],
|
||||
netbox_conf.getboolean('tls_verify'), general_conf['nmap_args'],
|
||||
tacacs_conf, general_conf['tag'], general_conf['unknown'])
|
||||
logging.info('started: {} networks'.format(len(networks)))
|
||||
nbs.sync(networks)
|
||||
logging.info('finished: +{} ~{} -{} ?{} !{}'.format(nbs.stats['created'],
|
||||
nbs.stats['updated'], nbs.stats['deleted'], nbs.stats['undiscovered'],
|
||||
nbs.stats['duplicated']))
|
||||
|
||||
exit(0)
|
||||
exit(0)
|
||||
|
Loading…
x
Reference in New Issue
Block a user