Skip to content

Commit af65c25

Browse files
committed
Merge branch 'bile0026-master'
merging code improvements and v2.9 support
2 parents 6569da7 + b1ad076 commit af65c25

9 files changed

+109
-600
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,9 @@ venv.bak/
106106
# trash
107107
.vscode/
108108

109+
scans/*
110+
logs/*
111+
112+
test_api.py
113+
114+
samples/custom-nmap-scan.sh

nbs/__init__.py

+32-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
11
import logging
2+
import requests
23

34
from pynetbox import api
45

56

67
class NetBoxScanner(object):
78

89
def __init__(self, address, token, tls_verify, tag, cleanup):
9-
self.netbox = api(address, token, ssl_verify=tls_verify)
10-
self.tag = tag
11-
self.cleanup = cleanup
12-
self.stats = {
13-
'unchanged': 0,
14-
'created': 0,
15-
'updated': 0,
16-
'deleted': 0,
17-
'errors': 0
18-
}
10+
if (tls_verify == 'no'):
11+
session = requests.Session()
12+
session.verify = False
13+
self.netbox = api(address, token)
14+
self.netbox.http_session = session
15+
self.tag = tag
16+
self.cleanup = cleanup
17+
self.stats = {
18+
'unchanged': 0,
19+
'created': 0,
20+
'updated': 0,
21+
'deleted': 0,
22+
'errors': 0
23+
}
24+
else:
25+
self.netbox = api(address, token)
26+
self.tag = tag
27+
self.cleanup = cleanup
28+
self.stats = {
29+
'unchanged': 0,
30+
'created': 0,
31+
'updated': 0,
32+
'deleted': 0,
33+
'errors': 0
34+
}
1935

2036
def sync_host(self, host):
2137
'''Syncs a single host to NetBox
@@ -36,18 +52,20 @@ def sync_host(self, host):
3652
aux = nbhost.description
3753
nbhost.description = host[1]
3854
nbhost.save()
39-
logging.info(f'updated: {host[0]}/32 "{aux}" -> "{host[1]}"')
55+
logging.info(
56+
f'updated: {host[0]}/32 "{aux}" -> "{host[1]}"')
4057
self.stats['updated'] += 1
4158
else:
4259
logging.info(f'unchanged: {host[0]}/32 "{host[1]}"')
4360
self.stats['unchanged'] += 1
4461
else:
45-
logging.info(f'unchanged: {host[0]}/32 "{host[1]}"')
46-
self.stats['unchanged'] += 1
62+
logging.info(f'unchanged: {host[0]}/32 "{host[1]}"')
63+
self.stats['unchanged'] += 1
4764
else:
4865
self.netbox.ipam.ip_addresses.create(
4966
address=host[0],
50-
# tags=[self.tag],
67+
tags=[{"name": self.tag}],
68+
# dns_name=host[1],
5169
description=host[1]
5270
)
5371
logging.info(f'created: {host[0]}/32 "{host[1]}"')

nbs/nmap.py

-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,3 @@ def run(self):
2828
host.find('address').attrib['addr'],
2929
self.unknown
3030
))
31-

netbox-scanner.conf

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
[NETBOX]
2-
address = https://netbox.domain
3-
token =
2+
address = <server>
3+
token = <token>
4+
logs = logs/
5+
# use lowercase no if you want to skip ssl verification.
6+
# any other value will verify the server ssl certificate.
47
tls_verify = no
5-
logs = .
68

79
[NMAP]
8-
path = /opt/netbox-scanner/samples/nmap
10+
path = ./
911
unknown = autodiscovered:netbox-scanner
1012
tag = nmap
11-
cleanup = yes
13+
cleanup = no
1214

1315
[NETXMS]
1416
address = https://netxms.domain
15-
username =
17+
username =
1618
password =
17-
tls_verify = no
1819
unknown = autodiscovered:netbox-scanner
1920
tag = netxms
2021
cleanup = yes
2122

2223
[PRIME]
2324
address = https://prime.domain/webacs/api/v4
24-
username =
25-
password =
26-
tls_verify = no
25+
username =
26+
password =
2727
unknown = autodiscovered:netbox-scanner
2828
tag = prime
2929
cleanup = yes

netbox-scanner.py

+30-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22

33
import logging
4+
import sys
45

56
from configparser import ConfigParser
67
from argparse import ArgumentParser
@@ -10,9 +11,15 @@
1011
from urllib3.exceptions import InsecureRequestWarning
1112

1213
from nbs import NetBoxScanner
13-
from nbs.nmap import Nmap
14-
from nbs.netxms import NetXMS
15-
from nbs.prime import Prime
14+
15+
argument = str(sys.argv[1])
16+
17+
if argument == 'nmap':
18+
from nbs.nmap import Nmap
19+
if argument == 'netxms':
20+
from nbs.netxms import NetXMS
21+
if argument == 'prime':
22+
from nbs.prime import Prime
1623

1724

1825
local_config = expanduser('~/.netbox-scanner.conf')
@@ -27,29 +34,36 @@
2734
raise FileNotFoundError('Configuration file was not found.')
2835

2936
netbox = config['NETBOX']
30-
nmap = config['NMAP']
31-
netxms = config['NETXMS']
32-
prime = config['PRIME']
37+
if argument == 'nmap':
38+
nmap = config['NMAP']
39+
if argument == 'netxms':
40+
netxms = config['NETXMS']
41+
if argument == 'prime':
42+
prime = config['PRIME']
3343

3444
parser = ArgumentParser(description='netbox-scanner')
3545
subparsers = parser.add_subparsers(title='Commands', dest='command')
3646
subparsers.required = True
37-
argsp = subparsers.add_parser('nmap', help='Nmap module')
38-
argsp = subparsers.add_parser('netxms', help='NetXMS module')
39-
argsp = subparsers.add_parser('prime', help='Cisco Prime module')
47+
if argument == 'nmap':
48+
argsp = subparsers.add_parser('nmap', help='Nmap module')
49+
if argument == 'netxms':
50+
argsp = subparsers.add_parser('netxms', help='NetXMS module')
51+
if argument == 'prime':
52+
argsp = subparsers.add_parser('prime', help='Cisco Prime module')
4053
args = parser.parse_args()
4154

4255
logfile = '{}/netbox-scanner-{}.log'.format(
4356
netbox['logs'],
4457
datetime.now().isoformat()
4558
)
4659
logging.basicConfig(
47-
filename=logfile,
48-
level=logging.INFO,
60+
filename=logfile,
61+
level=logging.INFO,
4962
format='%(asctime)s\tnetbox-scanner\t%(levelname)s\t%(message)s'
5063
)
5164
logging.getLogger().addHandler(logging.StreamHandler())
5265

66+
# useful if you have tls_verify set to no
5367
disable_warnings(InsecureRequestWarning)
5468

5569

@@ -58,6 +72,7 @@ def cmd_nmap(s): # nmap handler
5872
h.run()
5973
s.sync(h.hosts)
6074

75+
6176
def cmd_netxms(s): # netxms handler
6277
h = NetXMS(
6378
netxms['address'],
@@ -69,12 +84,13 @@ def cmd_netxms(s): # netxms handler
6984
h.run()
7085
s.sync(h.hosts)
7186

87+
7288
def cmd_prime(s): # prime handler
7389
h = Prime(
7490
prime['address'],
7591
prime['username'],
7692
prime['password'],
77-
prime.getboolean('tls_verify'),
93+
prime.getboolean('tls_verify'),
7894
prime['unknown']
7995
)
8096
h.run() # set access_point=True to process APs
@@ -85,8 +101,8 @@ def cmd_prime(s): # prime handler
85101
scanner = NetBoxScanner(
86102
netbox['address'],
87103
netbox['token'],
88-
netbox.getboolean('tls_verify'),
89-
nmap['tag'],
104+
netbox['tls_verify'],
105+
nmap['tag'],
90106
nmap.getboolean('cleanup')
91107
)
92108

samples/networks.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
192.168.0.0/24
2+
192.168.1.0/24
3+
192.168.2.0/24
4+
192.168.3.0/24
5+
172.16.0.0/24

samples/nmap-1.xml

-72
This file was deleted.

0 commit comments

Comments
 (0)