71 lines
2.3 KiB
Python
Executable File
71 lines
2.3 KiB
Python
Executable File
import socket
|
|
import subprocess
|
|
import os
|
|
import platform
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
def get_mac(ip):
|
|
try:
|
|
pid = os.getpid()
|
|
if platform.system() == "Windows":
|
|
arp_output = subprocess.check_output(f"arp -a {ip}", shell=True).decode()
|
|
else:
|
|
arp_output = subprocess.check_output(f"arp -n {ip}", shell=True).decode()
|
|
for line in arp_output.split('\n'):
|
|
if ip in line:
|
|
return line.split()[1] if platform.system() == "Windows" else line.split()[2]
|
|
except Exception as e:
|
|
return None
|
|
|
|
def scan_ip(ip):
|
|
hostname = None
|
|
mac_address = get_mac(ip)
|
|
try:
|
|
hostname = socket.gethostbyaddr(ip)[0]
|
|
except socket.herror:
|
|
pass
|
|
open_ports = scan_ports(ip)
|
|
return ip, hostname, mac_address, open_ports
|
|
|
|
def scan_ports(ip, start_port=1, end_port=1024):
|
|
open_ports = []
|
|
for port in range(start_port, end_port + 1):
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
sock.settimeout(0.5)
|
|
result = sock.connect_ex((ip, port))
|
|
if result == 0:
|
|
open_ports.append(port)
|
|
sock.close()
|
|
return open_ports
|
|
|
|
def ping(ip):
|
|
param = "-n" if platform.system().lower() == "windows" else "-c"
|
|
command = f"ping {param} 1 {ip}"
|
|
return subprocess.call(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) == 0
|
|
|
|
def get_network_ips(network):
|
|
ip_parts = network.split('.')
|
|
base_ip = ".".join(ip_parts[:-1]) + "."
|
|
return [base_ip + str(i) for i in range(1, 255)]
|
|
|
|
def scan_network(network):
|
|
devices = []
|
|
network_ips = get_network_ips(network)
|
|
with ThreadPoolExecutor(max_workers=50) as executor:
|
|
futures = [executor.submit(scan_ip, ip) for ip in network_ips if ping(ip)]
|
|
for future in futures:
|
|
devices.append(future.result())
|
|
return devices
|
|
|
|
if __name__ == "__main__":
|
|
network = "10.0.32.0/24" # Beispiel für ein Netzwerk, das gescannt werden soll
|
|
devices = scan_network(network)
|
|
|
|
for device in devices:
|
|
ip, hostname, mac, open_ports = device
|
|
print(f"IP: {ip}")
|
|
print(f"Hostname: {hostname}")
|
|
print(f"MAC-Adresse: {mac}")
|
|
print(f"Offene Ports: {open_ports}")
|
|
print("-" * 40)
|