Update scan.py
This commit is contained in:
parent
e5962964f4
commit
d905117db4
24
scan.py
24
scan.py
@ -1,11 +1,14 @@
|
||||
|
||||
import ipaddress
|
||||
import subprocess
|
||||
import argparse
|
||||
import csv
|
||||
import json
|
||||
import time
|
||||
import threading
|
||||
import itertools
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
|
||||
# Domyślny OID tylko sysName
|
||||
default_oids = {
|
||||
"sysName": "1.3.6.1.2.1.1.5.0",
|
||||
#"sysDescr": "1.3.6.1.2.1.1.1.0",
|
||||
@ -32,12 +35,15 @@ def query_all_oids(ip, community, oids, timeout=1):
|
||||
return (str(ip), values)
|
||||
return None
|
||||
|
||||
def scan_subnet(subnet, community, oids):
|
||||
def scan_subnet(subnet, community, oids, counter, total, lock):
|
||||
results = []
|
||||
for ip in subnet.hosts():
|
||||
result = query_all_oids(ip, community, oids, timeout=1)
|
||||
if result:
|
||||
results.append(result)
|
||||
with lock:
|
||||
counter[0] += 1
|
||||
print(f"\rSkanowanie IP: {counter[0]}/{total}", end="", flush=True)
|
||||
return results
|
||||
|
||||
def split_subnet(supernet, new_prefix=24):
|
||||
@ -82,9 +88,11 @@ def main():
|
||||
|
||||
oids = json.loads(args.oids) if args.oids else default_oids
|
||||
|
||||
all_input_subnets = list(args.subnets)
|
||||
all_input_subnets = []
|
||||
if args.subnet_file:
|
||||
all_input_subnets.extend(read_subnets_from_file(args.subnet_file))
|
||||
if args.subnets:
|
||||
all_input_subnets.extend(args.subnets)
|
||||
|
||||
if not all_input_subnets:
|
||||
print("Brak podsieci do przeskanowania (ani z linii poleceń, ani z pliku).")
|
||||
@ -94,21 +102,27 @@ def main():
|
||||
for subnet in all_input_subnets:
|
||||
subnets_to_scan.extend(split_subnet(subnet, new_prefix=args.prefix))
|
||||
|
||||
# Oblicz łączną liczbę hostów
|
||||
total_ips = sum(1 for subnet in subnets_to_scan for _ in subnet.hosts())
|
||||
counter = [0]
|
||||
lock = threading.Lock()
|
||||
|
||||
print(f"{'IP':<15} " + " ".join([f"{name:<25}" for name in oids.keys()]))
|
||||
print("-" * (15 + 26 * len(oids)))
|
||||
|
||||
all_results = []
|
||||
with ThreadPoolExecutor(max_workers=args.workers) as executor:
|
||||
futures = {
|
||||
executor.submit(scan_subnet, subnet, args.community, oids): subnet
|
||||
executor.submit(scan_subnet, subnet, args.community, oids, counter, total_ips, lock): subnet
|
||||
for subnet in subnets_to_scan
|
||||
}
|
||||
for future in as_completed(futures):
|
||||
subnet_results = future.result()
|
||||
for ip_str, values in subnet_results:
|
||||
all_results.append((ip_str, values))
|
||||
print(f"{ip_str:<15} " + " ".join([f"{values[name]:<25}" for name in oids.keys()]))
|
||||
print(f"\n{ip_str:<15} " + " ".join([f"{values[name]:<25}" for name in oids.keys()]))
|
||||
|
||||
print("\nSkanowanie zakończone.")
|
||||
save_to_csv(all_results, oids, filename=args.file)
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
Loading…
x
Reference in New Issue
Block a user