diff --git a/README.md b/README.md index 3bb956d..f1e6a47 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,13 @@ $ pip install -r requirements.txt After installation, use the `netbox-scanner.conf` file as an example to create your own and put this file in `/opt/netbox` or prepend its name with a dot and put it in your home directory --`~/.netbox-scanner.conf`. Keep reading to learn more about configuration. +> Starting with Netbox **v2.9.0** there are changes to the way tags are created. You must go into the web UI and explicity create a tag for each module you are planning to use here. So for example, if you want to use the nmap module, you have to create a Netbox tag called 'nmap' before you can successfully use it. + +## Quick Start + +0. Clone the repo and install the dependencies as shown above. +1. Move the `netbox-scanner.conf` file to your Netbox directory (`/opt/netbox`) and fill out the variables according to your setup. +2. Go to the `samples` subdirectory of this repo and execute `./nmap-scan.sh` to get a first look at the behavior of this project. ## Basics netbox-scanner reads a user-defined source to discover IP addresses and descriptions, and insert them into NetBox. To control what was previously inserted, netbox-scanner adds tags to each record, so it will know that that item can be handled. In order to guarantee the integrity of manual inputs, records without such tags will not be updated or removed. diff --git a/nbs/__init__.py b/nbs/__init__.py index d297ae6..38e18a9 100644 --- a/nbs/__init__.py +++ b/nbs/__init__.py @@ -4,7 +4,7 @@ from pynetbox import api class NetBoxScanner(object): - + def __init__(self, address, token, tls_verify, tag, cleanup): self.netbox = api(address, token, ssl_verify=tls_verify) self.tag = tag @@ -46,15 +46,15 @@ class NetBoxScanner(object): self.stats['unchanged'] += 1 else: self.netbox.ipam.ip_addresses.create( - address=host[0], - tags=[self.tag], + address=host[0], + # tags=[self.tag], description=host[1] ) logging.info(f'created: {host[0]}/32 "{host[1]}"') self.stats['created'] += 1 return True - + def garbage_collector(self, hosts): '''Removes records from NetBox not found in last sync''' nbhosts = self.netbox.ipam.ip_addresses.filter(tag=self.tag) @@ -83,8 +83,9 @@ class NetBoxScanner(object): self.stats['unchanged'], self.stats['created'], self.stats['updated'], - self.stats['deleted'], + self.stats['deleted'], self.stats['errors'] )) return True + diff --git a/netbox-scanner.conf b/netbox-scanner.conf index a3fe8b3..e99918f 100644 --- a/netbox-scanner.conf +++ b/netbox-scanner.conf @@ -5,7 +5,7 @@ tls_verify = no logs = . [NMAP] -path = samples/nmap +path = /opt/netbox-scanner/samples/nmap unknown = autodiscovered:netbox-scanner tag = nmap cleanup = yes diff --git a/samples/nmap-scan.sh b/samples/nmap-scan.sh index 1aaafe6..dc6d26b 100644 --- a/samples/nmap-scan.sh +++ b/samples/nmap-scan.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/usr/bin/env bash # # This is just an example. # @@ -16,11 +16,12 @@ # to look for XML files. ## -NETWORKS = "10.1.2.3/24 10.2.3.4/32 192.168.0.0/19" -TODAY="$(date +%d.%m.%yT%H:%M:%S%Z)" +NETWORKS="10.1.2.3/24 10.2.3.4/32 192.168.0.0/19" +TODAY="$(date +%d%m%yT%H%M%S%Z)" for net in $NETWORKS; do - nmap -T4 -O -F --host-timeout 30s -oX nmap-"$net".xml + rawNet="${net:0:-3}" + sudo nmap -T4 -O -F --host-timeout 30s -oX nmap-"$rawNet".xml "$net" done python ../netbox-scanner.py nmap diff --git a/samples/nmap/.gitkeep b/samples/nmap/.gitkeep new file mode 100644 index 0000000..e69de29