From 8b71aef87b4b047ed08272699d1eaa9f6cf72c26 Mon Sep 17 00:00:00 2001
From: Votre Nom <Vous@exemple.com>
Date: Mon, 9 Oct 2023 17:11:49 +0200
Subject: [PATCH] TP python

---
 sio2/SISR/Python/analog   | 27 +++++++++++++++++++++++++++
 sio2/SISR/Python/creatusr | 19 +++++++++++++++++++
 sio2/SISR/Python/remplace | 27 +++++++++++++++++++++++++++
 3 files changed, 73 insertions(+)
 create mode 100755 sio2/SISR/Python/analog
 create mode 100755 sio2/SISR/Python/creatusr
 create mode 100755 sio2/SISR/Python/remplace

diff --git a/sio2/SISR/Python/analog b/sio2/SISR/Python/analog
new file mode 100755
index 0000000..8080db6
--- /dev/null
+++ b/sio2/SISR/Python/analog
@@ -0,0 +1,27 @@
+#!/usr/bin/python3
+import sys
+import re
+#tab = []
+volume = {}
+
+regexp = "^(\S+) (\S+) (\S+) \[([^]]+)\] \"(\w+) (\S+).*\" (\d+) (\S+)"
+for line in sys.stdin: # on lit sur l’entrée standard
+    line = line.rstrip () # on enleve le retour ligne
+    res = re.match (regexp, line)
+    if res:
+        (host, rfc931, user, date, request, url, status, byte) = res.groups()
+        host = res.group (1)
+        byte =int(res.group (8))
+        if host in volume:
+            volume[host]= volume[host] + byte
+        else: 
+            volume[host] = byte 
+
+for host in volume.keys():
+    print(host, ":", volume[host])
+
+
+
+
+
+
diff --git a/sio2/SISR/Python/creatusr b/sio2/SISR/Python/creatusr
new file mode 100755
index 0000000..a2ecc08
--- /dev/null
+++ b/sio2/SISR/Python/creatusr
@@ -0,0 +1,19 @@
+#!/usr/bin/python3
+
+try:
+    fh = open("user.txt", "r")
+except:
+    print ("Fichier user.txt inconnu")
+else:
+    line = fh.readline ()
+    while line:
+        # use realine() to read next line
+        maligne = line.rstrip()
+        (login,complet)=maligne.split(":")
+        print(login , complet)
+        cmd = "useradd -m -c \""+ complet + "\" " + login + "-s /bin/bash/ "+login
+        print (cmd)
+        line = fh.readline()
+
+    fh.close()
+
diff --git a/sio2/SISR/Python/remplace b/sio2/SISR/Python/remplace
new file mode 100755
index 0000000..086fd9e
--- /dev/null
+++ b/sio2/SISR/Python/remplace
@@ -0,0 +1,27 @@
+import re
+import os
+
+def remplacer_avec_regex(nom_fichier, ancien_motif, nouveau_motif):
+    # Renommer le fichier d'origine
+    nouveau_nom = nom_fichier + ".old"
+    os.rename(nom_fichier, nouveau_nom)
+
+    # Lire le contenu du fichier original
+    with open(nouveau_nom, 'r') as fichier_in:
+        contenu = fichier_in.read()
+
+    # Utiliser une expression régulière pour effectuer le remplacement
+    contenu_modifie = re.sub(ancien_motif, nouveau_motif, contenu)
+
+    # Écrire le contenu modifié dans le fichier d'origine
+    with open(nom_fichier, 'w') as fichier_out:
+        fichier_out.write(contenu_modifie)
+
+# Demander à l'utilisateur les informations nécessaires
+nom_fichier = input("Entrez le nom du fichier : ")
+ancien_motif = input("Entrez l'ancien motif à remplacer (expression régulière) : ")
+nouveau_motif = input("Entrez le nouveau motif : ")
+
+# Appeler la fonction
+remplacer_avec_regex(nom_fichier, ancien_motif, nouveau_motif)
+