add 2 metoths
This commit is contained in:
67
scan.py
Normal file
67
scan.py
Normal file
@@ -0,0 +1,67 @@
|
||||
import os
|
||||
import socket
|
||||
import nmap
|
||||
import requests
|
||||
from datetime import datetime
|
||||
|
||||
requests.packages.urllib3.disable_warnings()
|
||||
os.environ['PYTHONHTTPSVERIFY'] = '0'
|
||||
|
||||
DEFAULT_NETWORKS = "192.168.85.0/24,192.168.86.0/24"
|
||||
|
||||
|
||||
def load_networks():
|
||||
networks = os.getenv("SCAN_NETWORKS", DEFAULT_NETWORKS)
|
||||
return [network.strip() for network in networks.split(",") if network.strip()]
|
||||
|
||||
|
||||
def scan_network(network):
|
||||
print(f"Scanning network: {network}")
|
||||
nm = nmap.PortScanner()
|
||||
nm.scan(hosts=network, arguments='-p 1-32768 -T4 --host-timeout 2m')
|
||||
host_results = []
|
||||
|
||||
for host in nm.all_hosts():
|
||||
status = nm[host]["status"]["state"]
|
||||
ports = []
|
||||
if 'tcp' in nm[host]:
|
||||
ports = [
|
||||
port for port, info in nm[host]['tcp'].items()
|
||||
if info['state'] == 'open'
|
||||
]
|
||||
host_results.append((host, status, ports))
|
||||
print(f"Host: {host}, Status: {status}, Open ports: {' '.join(str(port) for port in ports)}")
|
||||
|
||||
return host_results
|
||||
|
||||
|
||||
def write_output(hosts, output_path):
|
||||
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
||||
header = f"# network.txt generated on {datetime.utcnow().isoformat()}Z\n"
|
||||
|
||||
with open(output_path, 'w', encoding='utf-8') as output_file:
|
||||
output_file.write(header)
|
||||
output_file.write("# host status open_ports\n")
|
||||
for host, status, ports in hosts:
|
||||
ports_text = ' '.join(str(port) for port in ports)
|
||||
output_file.write(f"{host} {status} {ports_text}\n")
|
||||
|
||||
print(f"Scan saved to {output_path}")
|
||||
|
||||
|
||||
def main():
|
||||
networks = load_networks()
|
||||
all_hosts = []
|
||||
|
||||
for network in networks:
|
||||
try:
|
||||
all_hosts.extend(scan_network(network))
|
||||
except Exception as exc:
|
||||
print(f"Failed to scan {network}: {exc}")
|
||||
|
||||
output_path = os.getenv("OUTPUT_PATH", "/app/output/network.txt")
|
||||
write_output(all_hosts, output_path)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user