-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlsit.py
More file actions
158 lines (130 loc) · 5.78 KB
/
lsit.py
File metadata and controls
158 lines (130 loc) · 5.78 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
#!/usr/bin/env python3
import argparse
import json
import os
import threading
import logging
from flask import Flask, render_template, jsonify
from moteur_base import choisir_moteur
dossier_script = os.path.dirname(os.path.realpath(__file__))
chemin_changelog = os.path.join(dossier_script, "CHANGELOG.md")
version_lsit = "LSIT (version inconnue)"
try:
with open(chemin_changelog, "r", encoding="utf-8") as f:
for ligne in f:
if ligne.startswith("## v"):
version_brute = ligne.replace("##", "").strip()
version_lsit = f"LSIT {version_brute}"
break
except FileNotFoundError:
pass
parser = argparse.ArgumentParser(description="LSIT - Linux System Inventory Tool : Cartographie l'infrastructure locale.")
parser.add_argument("-v", "--version", action="version", version=version_lsit)
parser.add_argument("--format", choices=["txt", "json"], help="Génère directement un rapport sans passer par le menu")
parser.add_argument("--serve", action="store_true", help="Lance directement le tableau de bord web sur le port 8080")
args = parser.parse_args()
# Auto-détection de l'OS et initialisation du moteur approprié
moteur_actif = choisir_moteur()
def afficher_menu():
print("\n===================================")
print(f" {version_lsit}")
print("===================================")
print(" 1. Générer un rapport TXT")
print(" 2. Générer un rapport JSON")
print(" 3. Lancer le tableau de bord web")
print(" 4. Retour au menu principal")
print("===================================")
def mode_txt(donnees: dict) -> None:
with open("rapport_lsit.txt", "a") as f:
f.write(f"Date de l'audit : {donnees['date']}\n")
f.write(f"La cible a été identifiée. Nom de la machine : {donnees['machine']}\n")
f.write(f"Mémoire totale : {donnees['ram']}\n")
f.write(f"Modèle du CPU : {donnees['cpu']}\n")
f.write("\n===================================\n")
f.write(" PROCESSUS ACTIFS \n")
f.write("===================================\n")
f.write(donnees["processus"])
f.write("\n===================================\n")
f.write(" ARBORESCENCE DOSSIERS \n")
f.write("===================================\n")
f.write(donnees["arborescence"])
f.write("\n===================================\n")
f.write(" AUDIT DE SÉCURITÉ \n")
f.write("===================================\n")
f.write(f"Groupe Sudo : {donnees['securite_sudoers']}\n\n")
f.write("Ports ouverts :\n")
f.write(donnees["securite_ports"])
print("Rapport TXT généré avec succès !")
input("Appuyez sur Entrée pour revenir au menu...")
def mode_json(donnees: dict) -> None:
with open("rapport_lsit.json", "w") as f:
json.dump(donnees, f, indent=4)
print("Rapport JSON généré avec succès !")
input("Appuyez sur Entrée pour revenir au menu...")
def mode_serve() -> None:
app = Flask(__name__, template_folder=os.path.join(dossier_script, "templates"),
static_folder=os.path.join(dossier_script, "static"))
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
@app.route('/')
def dashboard():
donnees = moteur_actif.collecter_donnees()
return render_template('dashboard.html',
date_audit=donnees["date"],
hostname=donnees["machine"],
cpu_info=donnees["cpu"],
ram_info=donnees["ram"],
utilisateurs_sudo=donnees["securite_sudoers"],
ports_ouverts=donnees["securite_ports"],
arborescence=donnees["arborescence"],
processus_actifs=donnees["processus"],
stockage=donnees["stockage"],
uptime=donnees["uptime"],
load_average=donnees["load_average"],
version_lsit=version_lsit)
@app.route('/api/donnees')
def api_donnees():
donnees = moteur_actif.collecter_donnees()
donnees["version_lsit"] = version_lsit
return jsonify(donnees)
PORT = 8080
print(f"\nTableau de bord Flask disponible sur : http://localhost:{PORT}")
print('Tapez "exit" ou "end" pour arrêter le serveur et revenir au menu.\n')
thread_serveur = threading.Thread(target=app.run, kwargs={'host': '0.0.0.0', 'port': PORT, 'use_reloader': False})
thread_serveur.daemon = True
thread_serveur.start()
while True:
commande = input("> ").strip().lower()
if commande in ("exit", "end"):
print("Serveur arrêté. (Appuyez sur Entrée pour continuer)")
break
def main():
while True:
afficher_menu()
choix = input("Votre choix : ").strip().lower()
if choix in ("exit", "end", "4"):
break
elif choix == "1":
print("\nCollecte des données en cours...")
donnees = moteur_actif.collecter_donnees()
mode_txt(donnees)
elif choix == "2":
print("\nCollecte des données en cours...")
donnees = moteur_actif.collecter_donnees()
mode_json(donnees)
elif choix == "3":
print("\nDémarrage du serveur web...")
mode_serve()
else:
print("Choix invalide. Veuillez entrer 1, 2, 3 ou 4.")
if args.format or args.serve:
if args.serve:
mode_serve()
else:
donnees = moteur_actif.collecter_donnees()
if args.format == "json":
mode_json(donnees)
else:
mode_txt(donnees)
else:
main()