forked from lopes/netbox-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetbox-scanner.py
120 lines (98 loc) · 2.97 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
#!/usr/bin/env python3
import logging
import sys
from configparser import ConfigParser
from argparse import ArgumentParser
from os.path import expanduser, isfile
from datetime import datetime
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'
config = ConfigParser()
if isfile(local_config):
config.read(local_config)
elif isfile(global_config):
config.read(global_config)
else:
raise FileNotFoundError('Configuration file was not found.')
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['path'], nmap['unknown'])
h.run()
s.sync(h.hosts)
def cmd_netxms(s): # netxms handler
h = NetXMS(
netxms['address'],
netxms['username'],
netxms['password'],
netxms.getboolean('tls_verify'),
netxms['unknown']
)
h.run()
s.sync(h.hosts)
def cmd_prime(s): # prime handler
h = Prime(
prime['address'],
prime['username'],
prime['password'],
prime.getboolean('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'],
nmap.getboolean('cleanup')
)
if args.command == 'nmap':
cmd_nmap(scanner)
elif args.command == 'netxms':
scanner.tag = 'netxms'
scanner.cleanup = netxms.getboolean('cleanup')
cmd_netxms(scanner)
elif args.command == 'prime':
scanner.tag = prime['tag']
scanner.cleanup = prime.getboolean('cleanup')
cmd_prime(scanner)
exit(0)