siotp/sio2/sisr/11-python/creatusr.py
2021-09-30 16:36:58 +02:00

33 lines
867 B
Python
Executable File

#!/usr/lib/python3
import sys
import subprocess
import os
nbarg = len(sys.argv)
if nbarg != 2:
print ("Nombre d'argument invalide")
exit (1)
filename = sys.argv[1]
try:
fh = open(filename, "r")
except:
print ("Fichier ", filename," inconnu")
exit (2)
else:
line = fh.readline ()
while line:
nouvline = line.rstrip()
login,nomlong = nouvline.split(':')
cmd = "sudo useradd -m -d /home/"+login+" -c \'"+ no$
res = os.system ("getent passwd "+login)
if res != 0:
passw = "pwgen 4 1"
os.system (cmd)
os.system ("pwgen 4 1")
os.system ("echo "+login+passw"|chpasswd)
os.system ("echo "login+passw" >> "+filename+".p$
else:
print ("Utilisateur "+login+" déjà éxistant")
line = fh.readline()
fh.close()