import_sanef_ips: csv.reader par indice (evite conflit Nom/Nom duplique)
This commit is contained in:
parent
a66e0d853e
commit
c8a5f73616
@ -24,9 +24,33 @@ def main():
|
|||||||
print(f"[INFO] CSV: {args.csv_path}")
|
print(f"[INFO] CSV: {args.csv_path}")
|
||||||
|
|
||||||
with open(args.csv_path, "r", encoding="utf-8-sig", newline="") as f:
|
with open(args.csv_path, "r", encoding="utf-8-sig", newline="") as f:
|
||||||
reader = csv.DictReader(f)
|
sample = f.read(4096); f.seek(0)
|
||||||
|
delim = ";" if sample.count(";") > sample.count(",") else ","
|
||||||
|
reader = csv.reader(f, delimiter=delim)
|
||||||
|
header = next(reader)
|
||||||
rows = list(reader)
|
rows = list(reader)
|
||||||
print(f"[INFO] {len(rows)} lignes")
|
print(f"[INFO] {len(rows)} lignes (delim={delim!r})")
|
||||||
|
print(f"[INFO] Colonnes: {header[:12]}...")
|
||||||
|
|
||||||
|
def find_idx(candidates):
|
||||||
|
"""Retourne l'indice de la PREMIERE colonne matchant."""
|
||||||
|
for i, h in enumerate(header):
|
||||||
|
if any(cand.lower() == h.lower() for cand in candidates):
|
||||||
|
return i
|
||||||
|
return -1
|
||||||
|
|
||||||
|
idx_host = find_idx(["Machine virtuelle->Nom", "Machine virtuelle",
|
||||||
|
"Serveur->Nom", "Serveur",
|
||||||
|
"Système->Nom", "Nom"])
|
||||||
|
idx_ip = find_idx(["Adresse IP", "IP"])
|
||||||
|
idx_gw = find_idx(["Passerelle"])
|
||||||
|
idx_mask = find_idx(["Masque de sous réseau"])
|
||||||
|
idx_vrf = find_idx(["VRF"])
|
||||||
|
print(f"[INFO] Indices: host={idx_host} ip={idx_ip} gw={idx_gw} mask={idx_mask} vrf={idx_vrf}")
|
||||||
|
|
||||||
|
if idx_host == -1 or idx_ip == -1:
|
||||||
|
print("[ERR] Colonne hostname ou IP introuvable")
|
||||||
|
return
|
||||||
|
|
||||||
conn = engine.connect().execution_options(isolation_level="AUTOCOMMIT")
|
conn = engine.connect().execution_options(isolation_level="AUTOCOMMIT")
|
||||||
|
|
||||||
@ -38,17 +62,16 @@ def main():
|
|||||||
missing = 0
|
missing = 0
|
||||||
skipped = 0
|
skipped = 0
|
||||||
|
|
||||||
for r in rows:
|
for row in rows:
|
||||||
# Hostname peut venir de Machine virtuelle / Serveur / Nom selon l'export iTop
|
def cell(i):
|
||||||
vm_name = (r.get("Machine virtuelle->Nom") or r.get("Machine virtuelle")
|
return row[i].strip() if 0 <= i < len(row) else ""
|
||||||
or r.get("Serveur->Nom") or r.get("Serveur")
|
vm_name = cell(idx_host)
|
||||||
or r.get("Système->Nom") or r.get("Nom") or "").strip()
|
|
||||||
if vm_name:
|
if vm_name:
|
||||||
vm_name = vm_name.split(".")[0].lower()
|
vm_name = vm_name.split(".")[0].lower()
|
||||||
ip = (r.get("Adresse IP") or r.get("IP") or "").strip()
|
ip = cell(idx_ip)
|
||||||
gw = (r.get("Passerelle") or "").strip() or None
|
gw = cell(idx_gw) or None
|
||||||
mask = (r.get("Masque de sous réseau") or "").strip() or None
|
mask = cell(idx_mask) or None
|
||||||
vrf = (r.get("VRF") or "").strip() or None
|
vrf = cell(idx_vrf) or None
|
||||||
|
|
||||||
if not vm_name or not ip:
|
if not vm_name or not ip:
|
||||||
skipped += 1
|
skipped += 1
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user