-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmac_validator.py
More file actions
220 lines (187 loc) · 8.76 KB
/
Copy pathmac_validator.py
File metadata and controls
220 lines (187 loc) · 8.76 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
import tkinter as tk
from tkinter import ttk, scrolledtext, messagebox
import re
from collections import Counter
class MACValidator:
def __init__(self, root):
self.root = root
self.root.title("MAC Address Validator")
self.root.geometry("900x700")
self.root.configure(bg="#1a1a1a")
style = ttk.Style()
style.theme_use('clam')
style.configure('Title.TLabel', background='#1a1a1a', foreground='#ffffff', font=('Segoe UI', 24, 'bold'))
style.configure('Subtitle.TLabel', background='#1a1a1a', foreground='#888888', font=('Segoe UI', 12))
style.configure('Primary.TButton', background='#007aff', foreground='#ffffff', font=('Segoe UI', 13, 'bold'), borderwidth=0, focusthickness=0)
style.map('Primary.TButton', background=[('active', '#0051d5')])
title_frame = tk.Frame(root, bg="#1a1a1a")
title_frame.pack(fill="x", padx=30, pady=(30, 10))
ttk.Label(title_frame, text="Validador de Endereço MAC", style='Title.TLabel').pack(anchor="w")
ttk.Label(title_frame, text="Cole a lista de MACs abaixo para validação", style='Subtitle.TLabel').pack(anchor="w", pady=(5, 0))
input_frame = tk.Frame(root, bg="#1a1a1a")
input_frame.pack(fill="both", expand=True, padx=30, pady=(20, 10))
tk.Label(input_frame, text="Entrada", bg="#1a1a1a", fg="#ffffff", font=('Segoe UI', 13, 'bold'), anchor="w").pack(fill="x", pady=(0, 8))
self.input_text = scrolledtext.ScrolledText(
input_frame,
height=8,
bg="#2a2a2a",
fg="#e0e0e0",
insertbackground="#ffffff",
font=('Consolas', 12),
relief="flat",
padx=15,
pady=15
)
self.input_text.pack(fill="both", expand=True)
btn_frame = tk.Frame(root, bg="#1a1a1a")
btn_frame.pack(fill="x", padx=30, pady=10)
ttk.Button(btn_frame, text="Validar MACs", command=self.validate_macs, style='Primary.TButton').pack(fill="x", ipady=12)
result_frame = tk.Frame(root, bg="#1a1a1a")
result_frame.pack(fill="both", expand=True, padx=30, pady=(10, 30))
tk.Label(result_frame, text="Resultados", bg="#1a1a1a", fg="#ffffff", font=('Segoe UI', 13, 'bold'), anchor="w").pack(fill="x", pady=(0, 8))
self.notebook = ttk.Notebook(result_frame)
self.notebook.pack(fill="both", expand=True)
self.tab_mal = self.create_tab("Mal Formatados")
self.tab_dup = self.create_tab("Duplicados")
self.tab_comma_wrong = self.create_tab("Vírgula Errada")
self.tab_comma_dup = self.create_tab("Vírgula Dupla")
self.tab_valid = self.create_tab("Válidos")
def create_tab(self, title):
frame = tk.Frame(self.notebook, bg="#2a2a2a")
self.notebook.add(frame, text=title)
text = scrolledtext.ScrolledText(
frame,
bg="#2a2a2a",
fg="#e0e0e0",
font=('Consolas', 11),
relief="flat",
padx=15,
pady=15,
state="disabled"
)
text.pack(fill="both", expand=True, padx=5, pady=5)
return text
def extract_macs(self, raw_text):
"""Extract MAC addresses from text, handling commas inside MACs."""
macs_raw = []
lines = raw_text.split('\n')
for line in lines:
line = line.strip()
if not line:
continue
# Check if line contains a MAC with comma in wrong place
# Pattern: XX:XX,XX:XX:XX:XX:XX or similar
# We look for a pattern where comma is used instead of colon
if ':' in line and ',' in line:
# Try to reconstruct: replace comma with colon and see if it becomes valid
reconstructed = line.replace(',', ':')
parts = reconstructed.split(':')
if len(parts) == 6:
# Check if all parts are 2 hex chars
all_hex = all(len(p) == 2 and all(c in '0123456789ABCDEFabcdef' for c in p) for p in parts)
if all_hex:
# This is a MAC with comma in wrong place
macs_raw.append(line)
continue
# Normal case: split by comma
parts = line.split(',')
for part in parts:
cleaned = part.strip()
if cleaned:
macs_raw.append(cleaned)
return macs_raw
def validate_macs(self):
for tab in [self.tab_mal, self.tab_dup, self.tab_comma_wrong, self.tab_comma_dup, self.tab_valid]:
tab.config(state="normal")
tab.delete("1.0", tk.END)
tab.config(state="disabled")
raw_text = self.input_text.get("1.0", tk.END).strip()
if not raw_text:
messagebox.showwarning("Aviso", "Por favor, cole uma lista de MACs primeiro.")
return
macs_raw = self.extract_macs(raw_text)
# Valid MAC pattern: XX:XX:XX:XX:XX:XX
mac_pattern = re.compile(r'^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$')
mal = []
dup = []
comma_wrong = []
comma_dup = []
valid = []
mac_counter = Counter(macs_raw)
seen_for_dup = set()
for mac in macs_raw:
# Check for comma inside MAC (wrong place) - do this FIRST
if ',' in mac and ':' in mac:
comma_wrong.append((mac, "Vírgula encontrada dentro do MAC"))
continue
elif ',' in mac:
comma_wrong.append((mac, "Vírgula presente no MAC"))
continue
# Check for double comma near this MAC in raw text
if ',,' in raw_text:
idx = raw_text.find(mac)
if idx != -1:
start = max(0, idx - 3)
end = min(len(raw_text), idx + len(mac) + 3)
surrounding = raw_text[start:end]
if ',,' in surrounding and mac not in [m for m, _ in comma_dup]:
comma_dup.append((mac, "Vírgula dupla nas proximidades"))
continue
# Validate MAC format
if ':' in mac:
parts = mac.split(':')
if len(parts) != 6:
mal.append((mac, f"Deveria ter 6 grupos, encontrado {len(parts)}"))
continue
has_error = False
for part in parts:
if len(part) != 2:
mal.append((mac, f"Grupo '{part}' tem {len(part)} caracteres (deveria ter 2)"))
has_error = True
break
if has_error:
continue
if not mac_pattern.match(mac):
mal.append((mac, "Formato inválido (não é hexadecimal)"))
else:
if mac not in seen_for_dup:
seen_for_dup.add(mac)
if mac_counter[mac] > 1:
dup.append((mac, f"Aparece {mac_counter[mac]} vezes"))
else:
valid.append(mac)
else:
if len(mac) == 12 and all(c in '0123456789ABCDEFabcdef' for c in mac):
mal.append((mac, "MAC sem separadores (deveria usar :)"))
elif len(mac) == 17 and '-' in mac:
mal.append((mac, "Usa hífen em vez de dois-pontos"))
else:
mal.append((mac, f"Formato não reconhecido (comprimento: {len(mac)})"))
self.populate_tab(self.tab_mal, mal)
self.populate_tab(self.tab_dup, dup)
self.populate_tab(self.tab_comma_wrong, comma_wrong)
self.populate_tab(self.tab_comma_dup, comma_dup)
self.tab_valid.config(state="normal")
for mac in valid:
self.tab_valid.insert(tk.END, f"{mac}\n")
self.tab_valid.config(state="disabled")
self.update_titles(len(mal), len(dup), len(comma_wrong), len(comma_dup), len(valid))
def populate_tab(self, tab, items):
tab.config(state="normal")
for item in items:
if isinstance(item, tuple):
mac, problema = item
tab.insert(tk.END, f"{mac}\n -> {problema}\n\n")
else:
tab.insert(tk.END, f"{item}\n")
tab.config(state="disabled")
def update_titles(self, mal, dup, cw, cd, val):
self.notebook.tab(0, text=f"Malformed ({mal})")
self.notebook.tab(1, text=f"Duplicates ({dup})")
self.notebook.tab(2, text=f"Wrong Comma ({cw})")
self.notebook.tab(3, text=f"Double Comma ({cd})")
self.notebook.tab(4, text=f"Valid ({val})")
if __name__ == "__main__":
root = tk.Tk()
app = MACValidator(root)
root.mainloop()