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)