sisr-11-python3

This commit is contained in:
bruno
2021-09-30 16:36:58 +02:00
parent 613e5877d7
commit 0904bce7ad
7 changed files with 191 additions and 0 deletions

34
sio2/sisr/11-python/ex4.py Executable file
View File

@@ -0,0 +1,34 @@
#!/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 \'"+ nomlong+"\' -s /bin/bash "+ login
res = os.system ("getent passwd "+login)
if res != 0:
passw = "pwgen 4 1"
os.system (cmd)
mdp = string(os.system ("pwgen 4 1"))
os.system ("echo "+login+mdp+"|chpasswd")
os.system ("echo "+login+mdp+" > "+filename+".pwd")
else:
print ("Utilisateur "+login+" déjà éxistant")
os.system ("sudo userdel -r "+login)
line = fh.readline()
fh.close()