Skip to content

Commit eb663da

Browse files
committed
feat(cli): ajouter les arguments utilisateur et l'export json
1 parent 6d60738 commit eb663da

File tree

1 file changed

+41
-12
lines changed

1 file changed

+41
-12
lines changed

lsit.py

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
#!/usr/bin/env python3
22
import subprocess
3+
import argparse
4+
import json
5+
from datetime import datetime
6+
7+
parser = argparse.ArgumentParser(description="LSIT - Linux System Inventory Tool : Cartographie l'infrastructure locale.")
8+
parser.add_argument("-v", "--version", action="version", version="LSIT v1.0")
9+
parser.add_argument("--format", choices=["txt", "json"], default="txt", help="Format de sortie du rapport")
10+
args = parser.parse_args()
311

412
with open("/etc/hostname") as f:
513
hostname = f.read().strip()
@@ -27,17 +35,38 @@
2735
cmd_tree = subprocess.run(["tree", "-L", "2", "/home/vagrant"], capture_output=True, text=True)
2836
arborescence = cmd_tree.stdout
2937

30-
with open("rapport_lsit.txt", "a") as f:
31-
f.write(f"La cible a été identifiée. Nom de la machine : {hostname}\n")
32-
f.write(f"Mémoire totale : {ram_info}\n")
33-
f.write(f"Modèle du CPU : {cpu_info}\n")
38+
date_audit = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
39+
40+
if args.format == "json":
41+
donnees_audit = {
42+
"date": date_audit,
43+
"machine": hostname,
44+
"ram": ram_info,
45+
"cpu": cpu_info,
46+
"processus": processus_actifs,
47+
"arborescence": arborescence
48+
}
49+
50+
with open("rapport_lsit.json", "w") as f:
51+
json.dump(donnees_audit, f, indent=4)
52+
53+
print("Rapport JSON généré avec succès !")
54+
55+
else:
56+
with open("rapport_lsit.txt", "a") as f:
57+
f.write(f"Date de l'audit : {date_audit}\n")
58+
f.write(f"La cible a été identifiée. Nom de la machine : {hostname}\n")
59+
f.write(f"Mémoire totale : {ram_info}\n")
60+
f.write(f"Modèle du CPU : {cpu_info}\n")
61+
62+
f.write("\n===================================\n")
63+
f.write(" PROCESSUS ACTIFS \n")
64+
f.write("===================================\n")
65+
f.write(processus_actifs)
3466

35-
f.write("\n===================================\n")
36-
f.write(" PROCESSUS ACTIFS \n")
37-
f.write("===================================\n")
38-
f.write(processus_actifs)
67+
f.write("\n===================================\n")
68+
f.write(" ARBORESCENCE DOSSIERS \n")
69+
f.write("===================================\n")
70+
f.write(arborescence)
3971

40-
f.write("\n===================================\n")
41-
f.write(" ARBORESCENCE DOSSIERS \n")
42-
f.write("===================================\n")
43-
f.write(arborescence)
72+
print("Rapport TXT généré avec succès !")

0 commit comments

Comments
 (0)