forked from lopes/netbox-scanner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnetbox-scanner.py
143 lines (117 loc) · 3.64 KB
/
netbox-scanner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python3
import logging
import os
import sys
from argparse import ArgumentParser
from configparser import ConfigParser
from datetime import datetime
from os.path import expanduser, isfile
from urllib3 import disable_warnings
from urllib3.exceptions import InsecureRequestWarning
from nbs import NetBoxScanner
argument = str(sys.argv[1])
if argument == 'nmap':
from nbs.nmap import Nmap
if argument == 'netxms':
from nbs.netxms import NetXMS
if argument == 'prime':
from nbs.prime import Prime
local_config = expanduser('~/.netbox-scanner.conf')
global_config = '/opt/netbox/netbox-scanner.conf'
dir_config = './netbox-scanner.conf'
config = ConfigParser()
if isfile(local_config):
config.read(local_config)
elif isfile(global_config):
config.read(global_config)
elif isfile(dir_config):
config.read(dir_config)
else:
raise FileNotFoundError('Configuration file was not found.')
def check_env(conf):
conf_with_env = {}
for each_section in conf.sections():
section_dict = {}
for (k, v) in conf.items(each_section):
try:
section_dict.update({k: os.environ["{0}_{1}".format(each_section, str(k).upper())]})
except KeyError:
section_dict.update({k: v})
conf_with_env.update({each_section: section_dict})
return conf_with_env
config = check_env(config)
netbox = config['NETBOX']
if argument == 'nmap':
nmap = config['NMAP']
if argument == 'netxms':
netxms = config['NETXMS']
if argument == 'prime':
prime = config['PRIME']
parser = ArgumentParser(description='netbox-scanner')
subparsers = parser.add_subparsers(title='Commands', dest='command')
subparsers.required = True
if argument == 'nmap':
argsp = subparsers.add_parser('nmap', help='Nmap module')
if argument == 'netxms':
argsp = subparsers.add_parser('netxms', help='NetXMS module')
if argument == 'prime':
argsp = subparsers.add_parser('prime', help='Cisco Prime module')
args = parser.parse_args()
logfile = '{}/netbox-scanner-{}.log'.format(
netbox['logs'],
datetime.now().isoformat()
)
logging.basicConfig(
filename=logfile,
level=logging.INFO,
format='%(asctime)s\tnetbox-scanner\t%(levelname)s\t%(message)s'
)
logging.getLogger().addHandler(logging.StreamHandler())
# useful if you have tls_verify set to no
disable_warnings(InsecureRequestWarning)
def cmd_nmap(s): # nmap handler
h = Nmap(nmap['unknown'], nmap['networks'].split(","))
h.run()
s.sync(h.hosts)
def cmd_netxms(s): # netxms handler
h = NetXMS(
netxms['address'],
netxms['username'],
netxms['password'],
bool(netxms['tls_verify']),
netxms['unknown']
)
h.run()
s.sync(h.hosts)
def cmd_prime(s): # prime handler
h = Prime(
prime['address'],
prime['username'],
prime['password'],
bool(prime['tls_verify']),
prime['unknown']
)
h.run() # set access_point=True to process APs
s.sync(h.hosts)
if __name__ == '__main__':
scanner = NetBoxScanner(
netbox['address'],
netbox['token'],
netbox['tls_verify'],
nmap['tag'],
bool(nmap['cleanup'])
)
if args.command == 'nmap':
logging.info(f'Nmap scan started')
cmd_nmap(scanner)
elif args.command == 'netxms':
logging.info(f'netxms scan started')
scanner.tag = 'netxms'
scanner.cleanup = bool(netxms['cleanup'])
cmd_netxms(scanner)
elif args.command == 'prime':
logging.info(f'prime scan started')
scanner.tag = prime['tag']
scanner.cleanup = bool(prime['cleanup'])
cmd_prime(scanner)
exit(0)