Repository navigation
Expand file tree
/
Copy pathpyipscan.py
More file actions
241 lines (190 loc) · 7.27 KB
/
Copy pathpyipscan.py
File metadata and controls
241 lines (190 loc) · 7.27 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
"""TCP / UDP IP Scanner (pyipscan.py)
Ein grafischer IP-Scanner auf Basis von Python und Tkinter, mit dem IP-Bereiche (CIDR) auf einen bestimmten Port über TCP, UDP oder beide Protokolle (BOTH) geprüft werden können. Nach dem Scan werden gefundene Treffer mit – sofern im lokalen Subnetz ermittelbar – zugehörigen MAC-Adressen aus der ARP-Tabelle angezeigt.
Funktionsübersicht
CIDR-Scan: Unterstützt Netzwerke wie /24, /16 oder /8 über ipaddress.ip_network (Standardwert: 192.168.1.0/24).
Port- & Protokollauswahl: Prüft einen frei wählbaren Port (Standard: 80) wahlweise per TCP, UDP oder BOTH.
Multithreading: Nutzt eine Warteschlange (Queue) mit 100 parallelen Worker-Threads und einem Timeout von 0.5 Sekunden pro Verbindungsversuch.
MAC-Adressauflösung: Liest nach Abschluss des Scans über arp -a die lokale ARP-Tabelle aus und ordnet Treffern im selben Subnetz die MAC-Adresse zu (ansonsten "unbekannt").
Ergebnisansicht & Export in Zwischenablage: Zeigt Treffer tabellarisch (IP-Adresse, MAC-Adresse) an; Mehrfachauswahl und Kopieren (Strg+C oder Button „Auswahl kopieren“) im Tab-getrennten Format werden unterstützt.
Integrierter Info-Dialog: Enthält Hinweise zu CIDR-Größen, Protokollbeispielen (u. a. TCP/Modbus, UDP/BACnet), MAC-Sichtbarkeit, Bedienung und Nutzungsberechtigung.
Voraussetzungen & Start
Python 3 mit Standardbibliotheken (socket, threading, subprocess, ipaddress, queue, tkinter). Keine externen Drittanbieter-Pakete erforderlich.
Plattform: Auf Windows ausgerichtet (Fenstertitel und arp -a-Auswertung). """
import socket
import threading
import subprocess
import ipaddress
from queue import Queue
import tkinter as tk
from tkinter import ttk, messagebox
# ---------------- Konfiguration ----------------
THREADS = 100
TIMEOUT = 0.5
queue = Queue()
results = []
lock = threading.Lock()
# ---------------- Scan Funktionen ----------------
def tcp_scan(ip, port):
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(TIMEOUT)
s.connect((str(ip), port))
return True
except:
return False
def udp_scan(ip, port):
try:
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.settimeout(TIMEOUT)
s.sendto(b'\x00', (str(ip), port))
s.recvfrom(1024)
return True
except:
return False
def worker(port, mode):
while not queue.empty():
ip = queue.get()
found = False
if mode in ("TCP", "BOTH"):
found = tcp_scan(ip, port)
if not found and mode in ("UDP", "BOTH"):
found = udp_scan(ip, port)
if found:
with lock:
results.append(str(ip))
queue.task_done()
# ---------------- ARP / MAC ----------------
def get_arp_table():
arp = {}
try:
output = subprocess.check_output("arp -a", shell=True, text=True)
for line in output.splitlines():
parts = line.split()
if len(parts) >= 3 and "." in parts[0]:
arp[parts[0]] = parts[1]
except:
pass
return arp
# ---------------- GUI Funktionen ----------------
def start_scan():
results.clear()
for i in tree.get_children():
tree.delete(i)
net_input = ip_entry.get().strip()
port_input = port_entry.get().strip()
mode = protocol_var.get()
try:
network = ipaddress.ip_network(net_input, strict=False)
port = int(port_input)
except:
messagebox.showerror("Fehler", "Ungültiger IP-Bereich oder Port")
return
while not queue.empty():
queue.get()
for ip in network.hosts():
queue.put(ip)
for _ in range(THREADS):
threading.Thread(
target=worker,
args=(port, mode),
daemon=True
).start()
status.set("Scan läuft...")
root.after(200, check_finished)
def check_finished():
if queue.unfinished_tasks == 0:
arp = get_arp_table()
for ip in sorted(results):
mac = arp.get(ip, "unbekannt")
tree.insert("", "end", values=(ip, mac))
status.set(f"Scan abgeschlossen ({len(results)} Treffer)")
else:
root.after(200, check_finished)
def copy_selection(event=None):
selected = tree.selection()
if not selected:
return
lines = []
for item in selected:
ip, mac = tree.item(item, "values")
lines.append(f"{ip}\t{mac}")
text = "\n".join(lines)
root.clipboard_clear()
root.clipboard_append(text)
root.update()
def show_info():
info = tk.Toplevel(root)
info.title("Info")
info.geometry("750x500")
info.resizable(False, False)
text = tk.Text(info, wrap="word")
text.pack(fill="both", expand=True, padx=10, pady=10)
info_text = (
"TCP / UDP IP Scanner – Information\n\n"
"IP-Bereich (CIDR):\n"
" /24 = letzte Zahl wird gescannt, 254 Adressen in Sekunden (z.B. 192.168.1.x)\n"
" /16 = letzte zwei Zahlen werden gescannt, 64.516 Adressen in Minuten (z.B. 192.168.x.x)\n"
" /8 = letzte drei Zahlen werden gescannt, 16 Mio. Adressen >24h (z.B. 192.x.x.x)\n\n"
"Protokolle:\n"
" TCP – MODBUS \n"
" UDP – BACNET \n\n"
"MAC-Adressen:\n"
" Nur sichtbar im gleichen Subnetz\n"
" Werden aus der lokalen ARP-Tabelle gelesen\n\n"
"Bedienung:\n"
" Mehrere Einträge mit Shift/Strg markieren\n"
" Kopieren mit Strg+C oder Button\n\n"
"Hinweis:\n"
" Nur Netzwerke scannen, für die eine Berechtigung besteht.\n\n"
"ACHTUNG: Das Scannen von Netzwerken kann Sicherheits- bzw. Überwachungssysteme auslösen."
)
text.insert("1.0", info_text)
text.config(state="disabled")
ttk.Button(info, text="Schließen", command=info.destroy)\
.pack(pady=5)
# ---------------- GUI ----------------
root = tk.Tk()
root.title("TCP / UDP IP Scanner (Windows)")
root.geometry("640x480")
top = ttk.Frame(root, padding=10)
top.pack(fill="x")
ttk.Label(top, text="IP-Bereich:").grid(row=0, column=0, sticky="w")
ip_entry = ttk.Entry(top, width=24)
ip_entry.insert(0, "192.168.1.0/24")
ip_entry.grid(row=0, column=1, padx=5)
ttk.Label(top, text="Port:").grid(row=0, column=2, sticky="w")
port_entry = ttk.Entry(top, width=6)
port_entry.insert(0, "80")
port_entry.grid(row=0, column=3)
protocol_var = tk.StringVar(value="TCP")
protocol_box = ttk.Combobox(
top,
textvariable=protocol_var,
values=["TCP", "UDP", "BOTH"],
width=8,
state="readonly"
)
protocol_box.grid(row=0, column=4, padx=5)
ttk.Button(top, text="Scan starten", command=start_scan)\
.grid(row=0, column=5, padx=5)
ttk.Button(top, text="Info", command=show_info)\
.grid(row=0, column=6, padx=5)
tree = ttk.Treeview(
root,
columns=("IP", "MAC"),
show="headings",
selectmode="extended"
)
tree.heading("IP", text="IP-Adresse")
tree.heading("MAC", text="MAC-Adresse")
tree.pack(fill="both", expand=True, padx=10, pady=10)
tree.bind("<Control-c>", copy_selection)
ttk.Button(
root,
text="Auswahl kopieren",
command=copy_selection
).pack(anchor="e", padx=10, pady=(0, 5))
status = tk.StringVar(value="Bereit")
ttk.Label(root, textvariable=status)\
.pack(anchor="w", padx=10)
root.mainloop()