From 0b03893204a2d9487e3789cb157a19f09ba2707c Mon Sep 17 00:00:00 2001 From: roterschnee Date: Mon, 13 May 2019 11:41:12 +0200 Subject: [PATCH] Check for IP existence in netbox before blind deletion --- netbox-scanner/nbscanner.py | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/netbox-scanner/nbscanner.py b/netbox-scanner/nbscanner.py index 7712647..a55a954 100644 --- a/netbox-scanner/nbscanner.py +++ b/netbox-scanner/nbscanner.py @@ -154,21 +154,26 @@ class NetBoxScanner(object): hosts = self.scan(network) self.logger('scanned', net=network, hosts=len(hosts)) for host in hosts: - self.sync_host(host) + self.sync_host(host) + + ips = list() + ips.append(self.netbox.ipam.ip_addresses.all()) + 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 + if any(ip == address for ip in ips): + 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_csv(self, csvfile):