Repository navigation
Expand file tree
/
Copy pathnetwork_scanner.py
More file actions
1461 lines (1282 loc) · 57 KB
/
Copy pathnetwork_scanner.py
File metadata and controls
1461 lines (1282 loc) · 57 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import subprocess
import re
import json
import ipaddress
import paramiko
import traceback
import sys
import os
import threading
import time
import getpass
import requests
from datetime import datetime
from collections import defaultdict
# Optional SNMP support
try:
from pysnmp.hlapi import (
SnmpEngine, CommunityData, UsmUserData,
UdpTransportTarget, ContextData,
ObjectType, ObjectIdentity,
getCmd, nextCmd,
usmHMACMD5AuthProtocol, usmHMACSHAAuthProtocol,
usmDESPrivProtocol, usmAesCfb128Protocol,
)
SNMP_AVAILABLE = True
except ImportError:
SNMP_AVAILABLE = False
# ─────────────────────────────────────────────
# PATHS
# ─────────────────────────────────────────────
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
CONFIG_FILE = os.path.join(SCRIPT_DIR, "switches_config.json")
LOG_DIR = "/var/log/network_scanner"
# ─────────────────────────────────────────────
# ANSI COLORS
# ─────────────────────────────────────────────
class Color:
RED = "\033[91m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
CYAN = "\033[96m"
BOLD = "\033[1m"
RESET = "\033[0m"
GREY = "\033[90m"
ULINE = "\033[4m"
def print_header(text): print(f"\n{Color.BOLD}{Color.CYAN}{'─'*60}\n {text}\n{'─'*60}{Color.RESET}")
def print_ok(text): print(f"{Color.GREEN}[✓]{Color.RESET} {text}")
def print_info(text): print(f"{Color.CYAN}[i]{Color.RESET} {text}")
def print_warn(text): print(f"{Color.YELLOW}[!]{Color.RESET} {text}")
def print_err(text): print(f"{Color.RED}[✗]{Color.RESET} {text}")
# ─────────────────────────────────────────────
# CONFIG LOADER
# ─────────────────────────────────────────────
# Runtime state populated by load_config()
NET = {}
SWITCHES = {}
MODELS = {}
def load_config():
"""
Loads switches_config.json into NET, SWITCHES and MODELS.
If the file does not exist, creates a minimal default and loads it.
"""
global NET, SWITCHES, MODELS
if not os.path.isfile(CONFIG_FILE):
print_warn(f"Config file not found. Creating default: {CONFIG_FILE}")
_write_default_config()
try:
with open(CONFIG_FILE, "r", encoding="utf-8") as file_handle:
data = json.load(file_handle)
except json.JSONDecodeError as json_err:
print_err(f"Invalid JSON in {CONFIG_FILE}: {json_err}")
sys.exit(1)
NET = data.get("network", {})
SWITCHES = {}
for entry in data.get("switches", []):
sw_id = entry.get("id")
if sw_id is not None:
SWITCHES[sw_id] = {
"ip": entry.get("ip", ""),
"model_id": entry.get("model_id", "HUAWEI_VRP"),
"snmp": entry.get("snmp", {}),
}
MODELS = {}
for model in data.get("models", []):
model_id = model.get("id")
if model_id:
MODELS[model_id] = model
snmp_count = sum(1 for s in SWITCHES.values() if s.get("snmp", {}).get("enabled"))
snmp_label = f", {snmp_count} SNMP-enabled" if snmp_count else ""
print_ok(f"Config loaded — {len(SWITCHES)} switch(es){snmp_label}, {len(MODELS)} model(s) available.")
def _write_default_config():
"""Writes a minimal default switches_config.json."""
default = {
"network": {
"interface": "vmbr0",
"network_cidr": "192.168.0.0/18",
"dhcp_start": "192.168.3.1",
"dhcp_end": "192.168.8.254"
},
"switches": [
{"id": i, "ip": f"192.168.1.{i}", "model_id": "HUAWEI_VRP"}
for i in range(1, 9)
],
"models": [
{
"id": "HUAWEI_VRP",
"brand": "Huawei",
"description": "Huawei VRP — S310, S5700, S5720, S6720, CE series",
"mac_format": "huawei",
"port_keywords": ["GE", "ETH", "XGE"],
"uplink_keywords": ["10GE", "XGE"],
"error_strings": ["Error:", "Unrecognized command", "Wrong parameter"],
"commands": {
"find_mac": "display mac-address {mac}",
"port_config": "display current-configuration interface {port}",
"port_state": "display interface {port} | include current state",
"port_vlan": "display port vlan {port}",
"port_errors": "display interface {port} | include errors",
"arp_lookup": "display arp | include {mac}",
"lldp_neighbors": "display lldp neighbor interface {port} brief",
"poe_status": "display poe power interface {port}",
"version": "display version | include VRP",
"cpu_usage": "display cpu-usage"
}
}
]
}
with open(CONFIG_FILE, "w", encoding="utf-8") as file_handle:
json.dump(default, file_handle, indent=2)
def save_config():
"""Writes current NET, SWITCHES and MODELS back to switches_config.json."""
switches_list = []
for sw_id in sorted(SWITCHES.keys()):
entry = {"id": sw_id, "ip": SWITCHES[sw_id]["ip"], "model_id": SWITCHES[sw_id]["model_id"]}
snmp = SWITCHES[sw_id].get("snmp", {})
if snmp.get("enabled"):
entry["snmp"] = snmp
switches_list.append(entry)
models_list = []
for model_id in sorted(MODELS.keys()):
models_list.append(MODELS[model_id])
data = {
"network": NET,
"switches": switches_list,
"models": models_list,
}
with open(CONFIG_FILE, "w", encoding="utf-8") as file_handle:
json.dump(data, file_handle, indent=2)
print_ok(f"Config saved to {CONFIG_FILE}")
# ─────────────────────────────────────────────
# MODEL HELPERS
# ─────────────────────────────────────────────
def get_model(switch_ip):
"""Returns the model dict for a given switch IP."""
for sw_data in SWITCHES.values():
if sw_data["ip"] == switch_ip:
return MODELS.get(sw_data["model_id"])
return None
def get_model_by_id(model_id):
return MODELS.get(model_id)
def model_cmd(model, key, mac="", port=""):
"""Returns the formatted CLI command from the model definition."""
template = model.get("commands", {}).get(key, "")
return template.replace("{mac}", mac).replace("{port}", port)
def is_error_output(output, model):
"""Returns True if the output contains any known error strings for this model."""
for err_str in model.get("error_strings", []):
if err_str.lower() in output.lower():
return True
return False
# ─────────────────────────────────────────────
# MAC FORMATTING PER VENDOR
# ─────────────────────────────────────────────
def format_mac_for_model(clean_mac, mac_format):
"""
Converts a 12-char hex string to the MAC format expected by the vendor CLI.
huawei : 5081-4037-1a2a
cisco : 5081.4037.1a2a
colon : 50:81:40:37:1a:2a (default — Aruba, Juniper, MikroTik, D-Link, TP-Link)
"""
clean = clean_mac.lower()
if mac_format == "huawei":
return f"{clean[:4]}-{clean[4:8]}-{clean[8:12]}"
if mac_format == "cisco":
return f"{clean[:4]}.{clean[4:8]}.{clean[8:12]}"
return ":".join(clean[i:i+2] for i in range(0, 12, 2))
def normalize_mac(mac_str):
"""
Returns (clean_12_hex, upper_colon_display) or (None, None) if invalid.
clean_12 is the raw 12 hex chars used as the base for all further formatting.
"""
clean = mac_str.replace(":", "").replace("-", "").replace(".", "").lower()
if len(clean) != 12:
return None, None
upper = ":".join(clean[i:i+2].upper() for i in range(0, 12, 2))
return clean, upper
# ─────────────────────────────────────────────
# UTILITIES
# ─────────────────────────────────────────────
def is_in_dhcp_range(ip_str):
try:
ip = ipaddress.ip_address(ip_str)
start = ipaddress.ip_address(NET.get("dhcp_start", "0.0.0.0"))
end = ipaddress.ip_address(NET.get("dhcp_end", "0.0.0.0"))
return start <= ip <= end
except:
return False
def get_vendor(mac_upper):
"""Queries MAC vendor via macvendors.com public API."""
try:
response = requests.get(f"https://api.macvendors.com/{mac_upper}", timeout=4)
if response.status_code == 200:
return response.text.strip()
except:
pass
return "Unknown"
def save_log(content):
"""Saves a text log to LOG_DIR."""
os.makedirs(LOG_DIR, exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
path = os.path.join(LOG_DIR, f"session_{timestamp}.log")
with open(path, "w") as file_handle:
file_handle.write(content)
print_ok(f"Log saved: {path}")
def prompt_credentials():
username = input(" SSH Username : ").strip()
password = getpass.getpass(" SSH Password : ")
return username, password
def prompt_snmp_credentials():
"""
Checks which SNMP versions are needed across all switches
and prompts for the appropriate credentials.
Returns a dict with community (v2c) and/or v3 credentials.
"""
creds = {}
needs_v2c = False
needs_v3 = False
for sw_data in SWITCHES.values():
snmp = sw_data.get("snmp", {})
if snmp.get("enabled"):
ver = str(snmp.get("version", "2c"))
if ver == "2c":
needs_v2c = True
elif ver == "3":
needs_v3 = True
if not needs_v2c and not needs_v3:
return creds
print(f"\n {Color.BOLD}SNMP Credentials{Color.RESET}")
if needs_v2c:
community = input(" SNMPv2c Community : ").strip()
if not community:
community = "public"
creds["community"] = community
if needs_v3:
print(f" {Color.GREY}SNMPv3 credentials:{Color.RESET}")
creds["v3_username"] = input(" Username : ").strip()
creds["v3_auth_protocol"] = input(" Auth protocol (MD5/SHA) [SHA]: ").strip().upper() or "SHA"
creds["v3_auth_password"] = getpass.getpass(" Auth password : ")
creds["v3_priv_protocol"] = input(" Priv protocol (DES/AES) [AES]: ").strip().upper() or "AES"
creds["v3_priv_password"] = getpass.getpass(" Priv password : ")
return creds
def has_snmp_switches():
"""Returns True if any switch has SNMP enabled."""
return any(s.get("snmp", {}).get("enabled") for s in SWITCHES.values())
# ─────────────────────────────────────────────
# SSH: exec with invoke_shell fallback
# Required for Huawei switches that reject
# exec_command with "Unable to open channel"
# ─────────────────────────────────────────────
def ssh_exec(client, command, timeout=8):
"""
Tries open_session first; falls back to invoke_shell
for switches that reject direct exec_command.
"""
try:
transport = client.get_transport()
if transport and transport.is_active():
channel = transport.open_session()
channel.settimeout(timeout)
channel.exec_command(command)
output = b""
deadline = time.time() + timeout
while time.time() < deadline:
if channel.recv_ready():
output += channel.recv(65535)
elif channel.exit_status_ready():
break
else:
time.sleep(0.1)
channel.close()
return output.decode("utf-8", errors="ignore").strip()
except Exception:
pass
try:
shell = client.invoke_shell(width=512, height=512)
shell.settimeout(timeout)
time.sleep(0.8)
while shell.recv_ready():
shell.recv(65535)
shell.send(command + "\n")
time.sleep(2.0)
output = b""
deadline = time.time() + timeout
while time.time() < deadline:
if shell.recv_ready():
output += shell.recv(65535)
else:
time.sleep(0.2)
shell.close()
return output.decode("utf-8", errors="ignore").strip()
except Exception:
return ""
# ─────────────────────────────────────────────
# SNMP FUNCTIONS
# ─────────────────────────────────────────────
# Standard OIDs
OID_QBRIDGE_FDB_PORT = "1.3.6.1.2.1.17.7.1.2.2.1.2" # Q-BRIDGE-MIB dot1qTpFdbPort
OID_BRIDGE_FDB_PORT = "1.3.6.1.2.1.17.4.3.1.2" # BRIDGE-MIB dot1dTpFdbPort
OID_BASE_PORT_IFINDEX = "1.3.6.1.2.1.17.1.4.1.2" # dot1dBasePortIfIndex
OID_IF_NAME = "1.3.6.1.2.1.31.1.1.1.1" # IF-MIB ifName
OID_IF_DESCR = "1.3.6.1.2.1.2.2.1.2" # IF-MIB ifDescr
OID_SYS_DESCR = "1.3.6.1.2.1.1.1.0" # SNMPv2-MIB sysDescr
OID_SYS_NAME = "1.3.6.1.2.1.1.5.0" # SNMPv2-MIB sysName
OID_HR_CPU_LOAD = "1.3.6.1.2.1.25.3.3.1.2" # HOST-RESOURCES-MIB hrProcessorLoad
def _build_snmp_auth(snmp_config, snmp_creds):
"""Builds the pysnmp authentication object for a switch."""
version = str(snmp_config.get("version", "2c"))
if version == "2c":
community = snmp_creds.get("community", "public")
return CommunityData(community)
if version == "3":
auth_proto_map = {
"MD5": usmHMACMD5AuthProtocol,
"SHA": usmHMACSHAAuthProtocol,
}
priv_proto_map = {
"DES": usmDESPrivProtocol,
"AES": usmAesCfb128Protocol,
}
auth_proto = snmp_creds.get("v3_auth_protocol", "SHA").upper()
priv_proto = snmp_creds.get("v3_priv_protocol", "AES").upper()
return UsmUserData(
snmp_creds.get("v3_username", ""),
snmp_creds.get("v3_auth_password", ""),
snmp_creds.get("v3_priv_password", ""),
authProtocol=auth_proto_map.get(auth_proto, usmHMACSHAAuthProtocol),
privProtocol=priv_proto_map.get(priv_proto, usmAesCfb128Protocol),
)
return None
def _mac_to_oid_suffix(clean_mac):
"""Converts a 12-char hex MAC to OID decimal suffix: '80.129.64.55.26.42'."""
return ".".join(str(int(clean_mac[i:i+2], 16)) for i in range(0, 12, 2))
def _oid_suffix_to_mac(suffix_parts):
"""Converts OID decimal suffix parts (list of ints) to 12-char hex MAC."""
if len(suffix_parts) != 6:
return None
return "".join(f"{b:02x}" for b in suffix_parts)
def snmp_get_single(switch_ip, snmp_port, auth_data, oid_str):
"""Performs a single SNMP GET and returns the value or None."""
if not SNMP_AVAILABLE:
return None
try:
iterator = getCmd(
SnmpEngine(), auth_data,
UdpTransportTarget((switch_ip, snmp_port), timeout=5, retries=1),
ContextData(),
ObjectType(ObjectIdentity(oid_str)),
)
error_indication, error_status, _, var_binds = next(iterator)
if error_indication or error_status:
return None
for _, val in var_binds:
result = str(val)
if result and result.lower() not in ("", "nosuchobject", "nosuchinstance", "endofmibview"):
return result
except Exception:
pass
return None
def snmp_walk(switch_ip, snmp_port, auth_data, oid_str):
"""Walks an OID subtree and returns list of (oid_string, value) tuples."""
if not SNMP_AVAILABLE:
return []
results = []
try:
for (error_indication, error_status, _, var_binds) in nextCmd(
SnmpEngine(), auth_data,
UdpTransportTarget((switch_ip, snmp_port), timeout=5, retries=1),
ContextData(),
ObjectType(ObjectIdentity(oid_str)),
lexicographicMode=False,
):
if error_indication or error_status:
break
for oid, val in var_binds:
results.append((str(oid), str(val)))
except Exception:
pass
return results
def snmp_find_mac_port(switch_ip, snmp_config, snmp_creds, clean_mac):
"""
Searches for a MAC address via SNMP on a switch.
Returns (port_name, bridge_port) or (None, None) if not found.
Strategy:
1. Try Q-BRIDGE-MIB (dot1qTpFdbPort) — VLAN-aware, most modern switches
2. Fall back to BRIDGE-MIB (dot1dTpFdbPort) — legacy
3. Map bridge port → ifIndex → ifName to get physical port name
"""
auth_data = _build_snmp_auth(snmp_config, snmp_creds)
if auth_data is None:
return None, None
snmp_port = snmp_config.get("port", 161)
mac_suffix = _mac_to_oid_suffix(clean_mac)
bridge_port = None
# 1. Try Q-BRIDGE-MIB (index: VLAN_ID.MAC_OCTETS)
qbridge_results = snmp_walk(switch_ip, snmp_port, auth_data, OID_QBRIDGE_FDB_PORT)
for oid_str, val in qbridge_results:
parts = oid_str.split(".")
if len(parts) >= 6:
tail_mac = _oid_suffix_to_mac([int(p) for p in parts[-6:]])
if tail_mac and tail_mac.lower() == clean_mac.lower():
try:
bridge_port = int(val)
except (ValueError, TypeError):
pass
break
# 2. Fall back to BRIDGE-MIB (index: MAC_OCTETS directly)
if bridge_port is None:
direct_oid = f"{OID_BRIDGE_FDB_PORT}.{mac_suffix}"
result = snmp_get_single(switch_ip, snmp_port, auth_data, direct_oid)
if result is not None:
try:
bridge_port = int(result)
except (ValueError, TypeError):
pass
if bridge_port is None:
return None, None
# 3. Map bridge port → ifIndex
ifindex_oid = f"{OID_BASE_PORT_IFINDEX}.{bridge_port}"
ifindex_val = snmp_get_single(switch_ip, snmp_port, auth_data, ifindex_oid)
if ifindex_val is None:
return f"bridge-port-{bridge_port}", bridge_port
# 4. Map ifIndex → port name (try ifName first, then ifDescr)
ifindex = ifindex_val
port_name = snmp_get_single(switch_ip, snmp_port, auth_data, f"{OID_IF_NAME}.{ifindex}")
if not port_name:
port_name = snmp_get_single(switch_ip, snmp_port, auth_data, f"{OID_IF_DESCR}.{ifindex}")
if not port_name:
port_name = f"ifIndex-{ifindex}"
return port_name, bridge_port
def snmp_get_version(switch_ip, snmp_config, snmp_creds):
"""Returns sysDescr (first line, max 120 chars) via SNMP, or None."""
auth_data = _build_snmp_auth(snmp_config, snmp_creds)
if auth_data is None:
return None
result = snmp_get_single(switch_ip, snmp_config.get("port", 161), auth_data, OID_SYS_DESCR)
if result:
return result.splitlines()[0][:120]
return None
def snmp_get_cpu(switch_ip, snmp_config, snmp_creds):
"""Returns average CPU load via HOST-RESOURCES-MIB, or None."""
auth_data = _build_snmp_auth(snmp_config, snmp_creds)
if auth_data is None:
return None
results = snmp_walk(switch_ip, snmp_config.get("port", 161), auth_data, OID_HR_CPU_LOAD)
if results:
loads = []
for _, val in results:
try:
loads.append(int(val))
except (ValueError, TypeError):
pass
if loads:
avg = sum(loads) // len(loads)
return f"CPU {avg}% (SNMP, {len(loads)} core(s))"
return None
# ─────────────────────────────────────────────
# PORT DETECTION (model-aware)
# ─────────────────────────────────────────────
def detect_port_type(port_name, model):
"""
Returns "UPLINK/TRUNK" if the port name matches an uplink keyword,
otherwise returns "ACCESS".
"""
for kw in model.get("uplink_keywords", []):
if kw.upper() in port_name.upper():
return "UPLINK/TRUNK"
return "ACCESS"
def extract_port_from_line(line, model):
"""
Scans a MAC table output line and returns the first token that
matches one of the model's port_keywords.
"""
for part in line.split():
for kw in model.get("port_keywords", []):
if kw.upper() in part.upper():
return part
return None
# ─────────────────────────────────────────────
# PORT DETAILS (model-aware)
# ─────────────────────────────────────────────
def get_port_details(client, port, clean_mac, model):
"""Retrieves detailed port information using model-specific commands."""
mac_fmt = format_mac_for_model(clean_mac, model.get("mac_format", "colon"))
lines = ["\n" + "─"*60, f" PORT DETAILS: {port}", "─"*60]
checks = [
("Current config", "port_config"),
("Physical state", "port_state"),
("Port VLAN", "port_vlan"),
("Interface errors", "port_errors"),
("ARP table match", "arp_lookup"),
("LLDP neighbors", "lldp_neighbors"),
("PoE consumption", "poe_status"),
]
for title, cmd_key in checks:
cmd = model_cmd(model, cmd_key, mac=mac_fmt, port=port)
if not cmd:
continue
try:
output = ssh_exec(client, cmd)
if output and not is_error_output(output, model):
lines.append(f"\n ▶ {title}:\n{output}")
except:
pass
return "\n".join(lines)
def get_port_vlan(client, port, model):
"""Extracts the first VLAN number from the port VLAN command output."""
cmd = model_cmd(model, "port_vlan", port=port)
if not cmd:
return "N/A"
try:
output = ssh_exec(client, cmd)
for line in output.splitlines():
vlan_match = re.search(r"\b(\d+)\b", line)
if vlan_match:
return vlan_match.group(1)
except:
pass
return "N/A"
# ─────────────────────────────────────────────
# SEARCH MAC ON A SINGLE SWITCH (thread worker)
# Tries SSH first, falls back to SNMP if
# the switch has SNMP enabled in its config.
# ─────────────────────────────────────────────
def search_mac_on_switch(switch_num, switch_ip, clean_mac, username, password,
snmp_creds, results, lock):
sw_data = SWITCHES.get(switch_num, {})
model = get_model(switch_ip)
snmp_cfg = sw_data.get("snmp", {})
if model is None:
with lock:
print_warn(f"Switch #{switch_num} ({switch_ip}) — no model defined in config, skipping.")
return
mac_fmt = format_mac_for_model(clean_mac, model.get("mac_format", "colon"))
ssh_ok = False
found_ssh = False
# ── Try SSH first ──
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(
switch_ip,
username=username,
password=password,
timeout=8,
allow_agent=False,
look_for_keys=False,
)
ssh_ok = True
output = ssh_exec(client, model_cmd(model, "find_mac", mac=mac_fmt))
if mac_fmt.lower() in output.lower():
for line in output.splitlines():
if mac_fmt.lower() in line.lower():
port = extract_port_from_line(line, model)
if port:
found_ssh = True
port_type = detect_port_type(port, model)
vlan = get_port_vlan(client, port, model)
details = (
get_port_details(client, port, clean_mac, model)
if port_type == "ACCESS" else ""
)
with lock:
results.append({
"switch_num": switch_num,
"switch_ip": switch_ip,
"model_id": model["id"],
"brand": model.get("brand", ""),
"port": port,
"port_type": port_type,
"vlan": vlan,
"details": details,
"via": "SSH",
})
client.close()
except paramiko.AuthenticationException:
with lock:
print_err(f"Switch #{switch_num} ({switch_ip}) — SSH authentication failed.")
except Exception:
with lock:
if not snmp_cfg.get("enabled"):
print(f"\n{Color.RED}{'!'*20} ERROR on Switch #{switch_num} ({switch_ip}) {'!'*20}{Color.RESET}")
traceback.print_exc()
# ── SNMP fallback ──
if not found_ssh and snmp_cfg.get("enabled") and SNMP_AVAILABLE and snmp_creds:
fallback_reason = "SSH found no match" if ssh_ok else "SSH connection failed"
with lock:
print_info(f"Switch #{switch_num} ({switch_ip}) — {fallback_reason}, trying SNMP...")
port_name, _ = snmp_find_mac_port(switch_ip, snmp_cfg, snmp_creds, clean_mac)
if port_name:
port_type = detect_port_type(port_name, model)
with lock:
results.append({
"switch_num": switch_num,
"switch_ip": switch_ip,
"model_id": model["id"],
"brand": model.get("brand", ""),
"port": port_name,
"port_type": port_type,
"vlan": "N/A (SNMP)",
"details": "",
"via": "SNMP",
})
# ─────────────────────────────────────────────
# SEARCH MAC (parallel across all switches)
# ─────────────────────────────────────────────
def search_mac(mac_input, username, password, snmp_creds=None, log_lines=None):
clean_mac, mac_upper = normalize_mac(mac_input)
if not clean_mac:
print_err("Invalid MAC format.")
return
print_header(f"MAC Trace: {mac_upper}")
vendor = get_vendor(mac_upper)
print_info(f"Vendor : {Color.YELLOW}{vendor}{Color.RESET}")
print_info(f"Searching on {len(SWITCHES)} switch(es) in parallel...")
results = []
lock = threading.Lock()
threads = []
for sw_id, sw_data in SWITCHES.items():
thread = threading.Thread(
target=search_mac_on_switch,
args=(sw_id, sw_data["ip"], clean_mac, username, password,
snmp_creds, results, lock),
daemon=True,
)
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
results.sort(key=lambda x: x["switch_num"])
if not results:
print_warn("MAC not found on any switch.")
return
print()
for entry in results:
via_tag = entry.get("via", "SSH")
label = (f"Switch #{entry['switch_num']} ({entry['switch_ip']})"
f" [{entry['brand']} / {entry['model_id']}]")
if entry["port_type"] == "ACCESS":
print(f"{Color.GREEN}{'='*60}{Color.RESET}")
print(f" {Color.BOLD}MAC FOUND ON ACCESS PORT{Color.RESET} {Color.GREY}(via {via_tag}){Color.RESET}")
print(f" {Color.BOLD}Switch :{Color.RESET} {label}")
print(f" {Color.BOLD}Port :{Color.RESET} {entry['port']}")
print(f" {Color.BOLD}VLAN :{Color.RESET} {entry['vlan']}")
print(f" {Color.BOLD}Vendor :{Color.RESET} {vendor}")
print(f"{Color.GREEN}{'='*60}{Color.RESET}")
if entry["details"]:
print(entry["details"])
else:
print_info(
f"MAC seen on {label} → Port {entry['port']} "
f"({Color.YELLOW}{entry['port_type']}{Color.RESET}), "
f"VLAN: {entry['vlan']} {Color.GREY}[via {via_tag}]{Color.RESET}"
)
if log_lines is not None:
log_lines.append(
f"MAC {mac_upper} | Switch #{entry['switch_num']} ({entry['switch_ip']}) "
f"[{entry['model_id']}] | Port {entry['port']} | "
f"Type {entry['port_type']} | VLAN {entry['vlan']} | "
f"Vendor {vendor} | via {via_tag}"
)
# ─────────────────────────────────────────────
# SEARCH BY IP
# ─────────────────────────────────────────────
def search_by_ip(username, password, snmp_creds=None):
target_ip = input("Enter IP to locate: ").strip()
print_header(f"Search by IP: {target_ip}")
found_mac = None
try:
proc = subprocess.run(["arp", "-n", target_ip], capture_output=True, text=True)
for line in proc.stdout.splitlines():
arp_match = re.search(r"([0-9a-fA-F]{2}[:\-]){5}[0-9a-fA-F]{2}", line)
if arp_match:
found_mac = arp_match.group()
break
except:
pass
if not found_mac:
print_info("Not in local ARP table, trying targeted arp-scan...")
try:
proc = subprocess.run(
["arp-scan", f"--interface={NET.get('interface','eth0')}", target_ip, "--plain"],
capture_output=True, text=True,
)
for line in proc.stdout.splitlines():
parts = line.split("\t")
if len(parts) >= 2 and parts[0].strip() == target_ip:
found_mac = parts[1].strip()
break
except:
pass
if not found_mac:
print_err(f"Could not resolve MAC for {target_ip}.")
return
print_ok(f"Resolved MAC: {found_mac}")
search_mac(found_mac, username, password, snmp_creds)
# ─────────────────────────────────────────────
# SEARCH MULTIPLE MACs
# ─────────────────────────────────────────────
def search_multiple_macs(username, password, snmp_creds=None):
print_header("Multiple MAC Search")
print("Enter MACs separated by commas or one per line. Empty line to finish.\n")
mac_list = []
while True:
line = input("> ").strip()
if not line:
break
for item in line.split(","):
item = item.strip()
if item:
mac_list.append(item)
if not mac_list:
print_warn("No MACs entered.")
return
log_lines = [f"=== Multiple MAC search {datetime.now()} ==="]
for mac in mac_list:
search_mac(mac, username, password, snmp_creds, log_lines)
if input("\nSave results to log? (y/n): ").strip().lower() == "y":
save_log("\n".join(log_lines))
# ─────────────────────────────────────────────
# ARP SCAN
# NOTE: Devices in the DHCP range may have
# either static or DHCP-assigned IPs. This tool
# uses ARP only and does NOT query the DHCP
# server. Cross-check with your DHCP lease table.
# ─────────────────────────────────────────────
def run_arp_scan():
interface = NET.get("interface", "eth0")
network_cidr = NET.get("network_cidr", "192.168.0.0/24")
print_header(f"ARP Scan — {network_cidr} / Interface: {interface}")
print_info(f"DHCP range: {NET.get('dhcp_start')} → {NET.get('dhcp_end')}")
print_info("Note: IPs in DHCP range may be static OR DHCP-assigned.")
print_info("Cross-check with your DHCP server to confirm static assignments.")
cmd = [
"arp-scan",
f"--interface={interface}",
network_cidr,
"--retry=2", "--timeout=600", "--plain",
]
try:
proc = subprocess.run(cmd, capture_output=True, text=True, check=True)
except Exception as scan_error:
print_err(f"arp-scan error: {scan_error}")
return
mac_to_ips = defaultdict(set)
for line in proc.stdout.splitlines():
parts = line.split("\t")
if len(parts) >= 2:
ip = parts[0].strip()
mac = parts[1].strip().upper()
mac_to_ips[mac].add(ip)
ips_in_dhcp_range = []
for mac, ips in mac_to_ips.items():
for ip in ips:
if is_in_dhcp_range(ip):
ips_in_dhcp_range.append((ip, mac))
duplicates = [(mac, ips) for mac, ips in mac_to_ips.items() if len(ips) > 1]
total = len(mac_to_ips)
if duplicates:
print(f"\n{Color.BOLD} MACs with multiple IPs:{Color.RESET}")
print(f" {'MAC ADDRESS':<20} {'#IPs':<6} {'In DHCP range':^14} IPs")
print(" " + "─" * 76)
for mac, ips in sorted(duplicates, key=lambda x: -len(x[1])):
in_range = any(is_in_dhcp_range(i) for i in ips)
flag = f"{Color.YELLOW}YES{Color.RESET}" if in_range else "No"
print(f" {mac:<20} {len(ips):<6} {flag:^14} {', '.join(list(ips)[:5])}")
if ips_in_dhcp_range:
print(f"\n{Color.BOLD}{Color.YELLOW} ⚠ IPs DETECTED IN DHCP RANGE (static or DHCP — verify with DHCP server):{Color.RESET}")
print(f" {'IP ADDRESS':<18} {'MAC ADDRESS'}")
print(" " + "─" * 42)
for ip, mac in sorted(ips_in_dhcp_range, key=lambda x: ipaddress.ip_address(x[0])):
print(f" {Color.YELLOW}{ip:<18}{Color.RESET} {mac}")
else:
print_ok("No IPs detected in the DHCP range.")
print(f"\n {Color.BOLD}Total MACs:{Color.RESET} {total} "
f"{Color.BOLD}Duplicates:{Color.RESET} {len(duplicates)} "
f"{Color.BOLD}In DHCP range:{Color.RESET} {len(ips_in_dhcp_range)}")
# ─────────────────────────────────────────────
# SWITCH CONNECTIVITY CHECK (model-aware)
# SSH first, SNMP fallback for version/CPU.
# ─────────────────────────────────────────────
def check_switch_connectivity(username, password, snmp_creds=None):
print_header("Switch Connectivity")
for sw_id, sw_data in sorted(SWITCHES.items()):
switch_ip = sw_data["ip"]
model = get_model_by_id(sw_data["model_id"])
snmp_cfg = sw_data.get("snmp", {})
brand = model.get("brand", sw_data["model_id"]) if model else sw_data["model_id"]
label = f"Switch #{sw_id} ({switch_ip}) [{brand} / {sw_data['model_id']}]"
ping = subprocess.run(
["ping", "-c", "1", "-W", "1", switch_ip],
capture_output=True
)
if ping.returncode != 0:
print_warn(f"{label} — NO RESPONSE (ping)")
continue
if model is None:
print_warn(f"{label} — model '{sw_data['model_id']}' not found in config.")
continue
ssh_ok = False
version_str = "N/A"
cpu_str = "N/A"
# ── Try SSH ──
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(
switch_ip,
username=username,
password=password,
timeout=8,
allow_agent=False,
look_for_keys=False,
)
ssh_ok = True
ver_output = ssh_exec(client, model_cmd(model, "version"))
ver_lines = [ln for ln in ver_output.splitlines() if ln.strip()]
if ver_lines:
version_str = ver_lines[0]
cpu_output = ssh_exec(client, model_cmd(model, "cpu_usage"))
for line in cpu_output.splitlines():
if "cpu" in line.lower():
cpu_str = line.strip()
break
client.close()
print_ok(f"{label} {Color.YELLOW}{version_str}{Color.RESET} | {cpu_str}")
except paramiko.AuthenticationException:
print_err(f"{label} — SSH authentication failed.")
except Exception as ssh_error:
if not snmp_cfg.get("enabled"):
print_err(f"{label} — SSH error: {ssh_error}")
# ── SNMP fallback ──
if not ssh_ok and snmp_cfg.get("enabled") and SNMP_AVAILABLE and snmp_creds:
ver = snmp_get_version(switch_ip, snmp_cfg, snmp_creds)
cpu = snmp_get_cpu(switch_ip, snmp_cfg, snmp_creds)
if ver:
version_str = ver
if cpu:
cpu_str = cpu
if ver or cpu:
print_ok(f"{label} {Color.YELLOW}{version_str}{Color.RESET} | {cpu_str} {Color.GREY}[SNMP]{Color.RESET}")
else:
print_err(f"{label} — SSH failed, SNMP returned no data.")
# ─────────────────────────────────────────────
# CONFIGURATION EDITOR
# ─────────────────────────────────────────────
EDITOR_MENU = """{bold}{cyan}╔════════════════════════════════════════╗
║ CONFIGURATION EDITOR ║
╠════════════════════════════════════════╣
║ [1] Edit network settings ║
║ [2] List switches ║
║ [3] Add switch ║
║ [4] Edit switch ║
║ [5] Delete switch ║
║ [6] List models ║
║ [7] Add model ║
║ [8] Edit model ║
║ [9] Delete model ║
║ [B] Back to main menu ║
╚════════════════════════════════════════╝{reset}"""
def _input_default(prompt, default):
"""Input with default value shown in brackets."""
val = input(f" {prompt} [{default}]: ").strip()
return val if val else str(default)
def _input_list(prompt, current_list):
"""Input for a comma-separated list with current values shown."""
current = ", ".join(current_list) if current_list else "(empty)"