-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdiscover.py
More file actions
executable file
·92 lines (67 loc) · 2.43 KB
/
discover.py
File metadata and controls
executable file
·92 lines (67 loc) · 2.43 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
#!/usr/bin/env python
import argparse
import store
import spreadsheet
import logging
import math
import threading
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s',
datefmt='%d/%m/%Y %I:%M:%S %p',
#filename='discover.log',
level=logging.DEBUG)
MAX_WORKERS = 10
lock = threading.Lock()
def check_params():
parser = argparse.ArgumentParser(description='Discover info of network' +
'devices.')
parser.add_argument('inputfile', help='excel file with hosts to scan')
return parser.parse_args()
def how_many_workers(items):
if len(items) < MAX_WORKERS:
workers = len(items)
else:
workers = MAX_WORKERS
return workers
def get_intervals(items, workers):
max_devices = int(math.ceil(len(items) / float(workers)))
return [items[x:x+max_devices] for x in range(0, len(items), max_devices)]
def scan(devices):
redis = store.get_redis()
logging.info("Starting worker to process {} devices".format(len(devices)))
for device in devices:
logging.info("Scanning device: {}".format(device))
device.scan(lock, redis=redis)
for e in device.errors:
print("ERROR ", device, e)
logging.info("Scanning result for device: {}".format(device))
if __name__ == "__main__":
args = check_params()
logging.info("Import excel")
devices = spreadsheet.import_from_excel(args.inputfile)
logging.info("Store devices in Redis")
store.save_devices(devices, flush=True)
workers = how_many_workers(devices)
intervals = get_intervals(devices, workers)
# start workers
logging.info("Starting workers")
threads = list()
for interval in intervals[:100]:
#t = threading.Thread(target=scan, args=(interval,))
#threads.append(t)
#t.start()
scan(interval)
# wait for all workers
logging.info("Waiting for all workers")
for t in threads:
t.join()
logging.info("All workers are done")
# Exporting data
devices = store.get_all_devices()
logging.info("Export to html")
store.export_html("html/output.html")
logging.info("Export to csv")
store.export_csv("devices.csv")
logging.info("Updating excel input")
spreadsheet.export_to_excel(devices, spreadsheet=args.inputfile)
logging.info("Exporting to excel")
spreadsheet.export_to_excel(devices)