scan_commands.py
12.2 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'eduardo'
import logging
import os
import os.path
import lxml.etree
import time
import pickle
from paste.script import command
from .. import Cocar
from ..model import Base
from ..model.network import Network
from ..model.printer import Printer, PrinterCounter
from ..model.host import Host
from ..model.computer import Computer
from ..csv_utils import NetworkCSV
from ..session import NmapSession, SnmpSession
from multiprocessing import Process, Queue
from ..xml_utils import NmapXML
from sqlalchemy.exc import IntegrityError
log = logging.getLogger()
class ScanCommands(command.Command):
"""
Comandos para realizar o scan da rede
Usage::
paster scan create_db -c <path to config file>
- Cria banco de dados
paster scan drop_db -c <path to config file>
- Remove banco de dados
paster scan networks -c <path to config file>
- Faz a busca das redes
Os comandos devem ser executados a partir da raiz do modulo Cocar
"""
max_args = 1
min_args = 1
summary = __doc__.split('\n')[0]
usage = __doc__
group_name = "Scan Commands"
parser = command.Command.standard_parser(verbose=True)
parser.add_option('-f', '--full',
action='store',
dest='full',
help='Full scan or regular scan'
)
def __init__(self, name):
"""
Constructor method
"""
super(ScanCommands, self).__init__(name)
self.cocar = Cocar(environment='production')
self.networks_dir = self.cocar.cocar_data_dir + "/networks"
self.networks_csv = self.cocar.config.get('cocar', 'networks_csv')
if not os.path.isdir(self.networks_dir):
os.mkdir(self.networks_dir)
def command(self):
"""
Parse command line arguments and call appropriate method.
"""
if not self.args or self.args[0] in ['--help', '-h', 'help']:
print(ScanCommands.__doc__)
return
cmd = self.args[0]
if cmd == 'create_db':
self.create_db()
return
if cmd == 'drop_db':
self.drop_db()
return
if cmd == 'load_networks':
self.load_networks()
return
if cmd == 'scan_networks':
self.scan_networks()
return
if cmd == 'load_network_files':
self.load_network_files()
return
if cmd == 'get_printers':
self.get_printers()
return
if cmd == 'continous_scan':
self.continuous_scan()
return
if cmd == 'printer_scan':
self.printer_scan()
return
else:
log.error('Command "%s" not recognized' % (cmd,))
def create_db(self):
"""
Create database
"""
Base.metadata.create_all(self.cocar.engine)
def drop_db(self):
"""
Drop database
"""
Base.metadata.drop_all(self.cocar.engine)
def load_networks(self):
"""
Load networks from CSV file
"""
networks_csv = NetworkCSV(csv_file=self.networks_csv)
session = self.cocar.Session
for elm in networks_csv.parse_csv():
results = session.query(Network).filter(Network.ip_network == elm.ip_network).first()
if results is None:
log.info("Adicionando a rede: %s", elm.network_ip)
session.add(elm)
else:
log.info("Rede já cadastrada: %s", elm.network_ip)
session.flush()
session.close()
def scan_networks(self):
"""
Scan all networks
"""
processes = int(self.cocar.config.get('cocar', 'processes'))
# Create queues
task_queue = Queue()
done_queue = Queue()
session = self.cocar.Session
results = session.query(Network).all()
for network in results:
network.network_ip = network.ip_network
if self.options.full is None:
nmap_session = NmapSession(
network.network_ip.cidr,
outfile=self.networks_dir + "/" + str(network.network_ip.cidr).replace("/", "-") + ".xml",
full=False
)
else:
nmap_session = NmapSession(
network.network_ip.cidr,
outfile=self.networks_dir + "/" + str(network.network_ip.cidr).replace("/", "-") + ".xml",
full=True
)
task_queue.put(nmap_session)
#Start worker processes
for i in range(processes):
Process(target=worker, args=(task_queue, done_queue)).start()
# Get and print results
print 'Unordered results:'
for i in range(len(results)):
print '\t', done_queue.get()
# Tell child processes to stop
for i in range(processes):
task_queue.put('STOP')
def load_network_files(self):
"""
Load printers from networks files
:return:
"""
onlyfiles = [ f for f in os.listdir(self.networks_dir) if os.path.isfile(os.path.join(self.networks_dir, f)) ]
for i in range(len(onlyfiles)):
network_file = self.networks_dir + "/" + onlyfiles[i]
log.info("Processando arquivo de rede %s", network_file)
nmap_xml = NmapXML(network_file)
try:
host_dict = nmap_xml.parse_xml()
except AttributeError, e:
log.error("Erro realizando parsing do arquivo %s\n%s", network_file, e.message)
continue
except lxml.etree.XMLSyntaxError, e:
log.error("Erro realizando parsing do arquivo %s\n%s", network_file, e.message)
continue
if not host_dict:
log.error("File %s not found", network_file)
continue
session = self.cocar.Session
for hostname in nmap_xml.hosts.keys():
host = nmap_xml.identify_host(hostname)
if isinstance(host, Printer):
# Vê se a impressora já está na base
results = session.query(Printer).filter(Printer.network_ip == hostname).first()
if results is None:
log.info("Inserindo impressora com o IP %s", hostname)
try:
session.add(host)
session.flush()
except IntegrityError, e:
log.error("Erro adicionando impressora com o IP %s. IP Repetido\n%s", hostname, e.message)
else:
log.info("Impressora com o IP %s já cadastrada", hostname)
elif isinstance(host, Computer):
# Vê se o host já está na base
results = session.query(Computer).filter(Computer.network_ip == hostname).first()
if results is None:
log.info("Inserindo computador com o IP %s", hostname)
try:
session.add(host)
session.flush()
except IntegrityError, e:
log.error("Erro adicionando computador com o IP %s. IP Repetido\n%s", hostname, e.message)
else:
log.info("Computador com o IP %s já cadastrado", hostname)
else:
# Insere host genérico
results = session.query(Host).filter(Host.network_ip == hostname).first()
if results is None:
log.info("Inserindo host genérico com o IP %s", hostname)
try:
session.add(host)
session.flush()
except IntegrityError, e:
log.error("Erro adicionando host genérico com o IP %s. IP Repetido\n%s", hostname, e.message)
else:
log.info("Host genérico com o IP %s já cadastrado", hostname)
#session.flush()
def get_printers(self):
"""
Read printers SNMP Information
:return:
"""
processes = int(self.cocar.config.get('cocar', 'processes'))
# Create queues
task_queue = Queue()
done_queue = Queue()
session = self.cocar.Session
results = session.query(Printer).all()
for printer in results:
log.info("Coletando informacoes da impressora %s", printer.network_ip)
#printer.network_ip = printer.ip_network
snmp_session = SnmpSession(
DestHost=printer.network_ip
)
if snmp_session is None:
log.error("Erro na coleta SNMP da impressora %s", printer.network_ip)
continue
else:
task_queue.put(snmp_session)
#Start worker processes
for i in range(processes):
Process(target=worker_printer, args=(task_queue, done_queue)).start()
# Get and print results
log.debug('Unordered results:')
for i in range(len(results)):
printer_dict = done_queue.get()
log.debug(printer_dict)
if printer_dict['counter'] is None:
log.error("Nao foi possivel ler o contador da impressora %s", printer_dict['network_ip'])
continue
try:
log.debug("Gravando contador = %s para a impressora = %s serial = %s", printer_dict['counter'], printer_dict['network_ip'], printer_dict['serial'])
printer = PrinterCounter(
ip_address=printer_dict['network_ip'],
model=printer_dict['model'],
serial=printer_dict['serial'],
description=printer_dict['description'],
counter=printer_dict['counter'],
counter_time=time.time()
)
printer.update_counter(session)
except AttributeError, e:
log.error("Erro na inserção do contador para a impressora %s\n%s", printer_dict['network_ip'], e.message)
continue
# Tell child processes to stop
for i in range(processes):
task_queue.put('STOP')
def continuous_scan(self):
"""
Fica varrendo a rede até parar por execução forçada
"""
print("*** Aperente CTRL+C para encerrar a execução ***")
while True:
log.info("Iniciando scan de redes...")
self.scan_networks()
log.info("Scan de redes finalizado. Iniciando procedimento de "
"identificação de ativos de rede, computadores e impressoras")
self.load_network_files()
log.info("SCAN DE REDE COMPLETO FINALIZADO!!!")
def printer_scan(self):
"""
Fica varrendo a rede e tenta encontrar as impressoras a cada 10min
"""
print("*** Aperente CTRL+C para encerrar a execução ***")
while True:
self.get_printers()
log.info("SCAN DE IMPRESSORAS FINALIZADO!!! Dormindo...")
time.sleep(600)
def make_query(host):
"""This does the actual snmp query
This is a bit fancy as it accepts both instances
of SnmpSession and host/ip addresses. This
allows a user to customize mass queries with
subsets of different hostnames and community strings
"""
return host.scan()
def make_query_printer(host):
"""This does the actual snmp query
This is a bit fancy as it accepts both instances
of SnmpSession and host/ip addresses. This
allows a user to customize mass queries with
subsets of different hostnames and community strings
"""
return host.printer_dict()
# Function run by worker processes
def worker(inp, output):
for func in iter(inp.get, 'STOP'):
result = make_query(func)
output.put(result)
# Function run by worker processes
def worker_printer(inp, output):
for func in iter(inp.get, 'STOP'):
result = make_query_printer(func)
output.put(result)