From c9237e9bf37b081e81d2ac01db66f784f215514e Mon Sep 17 00:00:00 2001 From: root Date: Fri, 4 Oct 2024 09:13:29 +0200 Subject: [PATCH] =?UTF-8?q?=09modifi=C3=A9=C2=A0:=20=20=20=20=20=20=20=20?= =?UTF-8?q?=20creatusr/creatusr.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sio2/SISR/07-python/creatusr/creatusr.py | 36 +++++++++++++++++++----- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/sio2/SISR/07-python/creatusr/creatusr.py b/sio2/SISR/07-python/creatusr/creatusr.py index 471ba85..88e1d2a 100644 --- a/sio2/SISR/07-python/creatusr/creatusr.py +++ b/sio2/SISR/07-python/creatusr/creatusr.py @@ -1,10 +1,32 @@ #!/usr/bin/python3 -try: - fh = open("users.txt", "r") -except: - print("Fichier inconnu") -else: - line = fh.readline() - while line: +import sys +import subprocess +def create_user(login, nomcomplet): + cmds = ["useradd", "-m", "-c", nomcomplet, "-s", "/bin/bash", login] + res = subprocess.run(cmds,stdout=subprocess.PIPE) + print ("createusr : utilisateur ", login, " cree") + +if len(sys.argv) != 2 : + print ("usage : ", sys.argv[0], " ") + exit (1) + +fichier = sys.argv[1] +try: + fh = open(fichier, "r") +except: + print (sys.argv[0], "erreur ouverture fichier ", fichier) + exit (2) +else: + line = fh.readline () + while line: + line = line.rstrip() +# print (line) + login,nomcomplet=line.split(':') + print(login," ", nomcomplet) + # use realine() to read next line + create_user(login, nomcomplet) + line = fh.readline () + fh.close() + exit (0)