|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | 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() |
3 | 11 |
|
4 | 12 | with open("/etc/hostname") as f: |
5 | 13 | hostname = f.read().strip() |
|
27 | 35 | cmd_tree = subprocess.run(["tree", "-L", "2", "/home/vagrant"], capture_output=True, text=True) |
28 | 36 | arborescence = cmd_tree.stdout |
29 | 37 |
|
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) |
34 | 66 |
|
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) |
39 | 71 |
|
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