create users in openldap from an SCV

0

Good morning everyone! I'm starting in the world of ubuntu and also Ldap and I have a problem because I want to make a script that allows me to create users through an SCV file which has three fields (name, surname and email). Once the user is loaded into the ldap then I have to migrate all the information in a Mysql file. In itself what I ask is that someone help me to see if the script is wrong or what can be the problem that users are not creating when I execute it. Finally I also ask for help to explain how I export to a mysql file

#!/usr/bin/python 
# -*- coding=utf-8 -*-
#enconding: utf-8
# -*- coding: 850 -*-
import smtplib
import ldap
import ldap.modlist as modlist
import getpass
import csv, operator
import urllib2 


from email.mime.text import MIMEText
from smtplib import SMTP 

#Solicitamos la password del admin de LDAP
password = getpass.getpass('Por favor ingrese su clave: ')

#Realizamos la conexion al servidor de ldap
uri = ldap.initialize("ldap://10.0.2.15:389")
uri.simple_bind_s("cn=admin,dc=ldap,dc=com",password)
uidNumber = 2010
gidNumber = 2001

#genero la nuevas contraseñas formatoRabdom
from random import SystemRandom
longitud = 18
valores = 
u"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
<=>@#%&+"
cryptogen = SystemRandom()
contrasena = ""
while longitud > 0:
    contrasena = contrasena + cryptogen.choice(valores)
    longitud = longitud - 1

#abro el archivo usuario y almaceno los datos en dos variables para 
#crear el usuario y enviar el correo
csvarchivo = open('/home/luis/ml/usuarios.csv')
entrada = csv.reader(csvarchivo)
for i in entrada:   
usuario = i[0]+i[1]
correo = i[2]

      #cargo al usuario en LDAP

dn= ("uid=%s,ou=people,dc=ldap,dc=com" %usuario)
attrs = {}
attrs['objectclass'] = 
['top','posixAccount','inetOrgPerson','ldapPublicKey']
attrs['cn'] = str(i[0])
attrs['uid'] = str(usuario)
attrs['sn'] = str(i[1]).encode('ascii','ignore')
attrs['uidNumber'] = str(uidNumber)
attrs['gidNUmber'] = str(gidNumber)
attrs['mail'] = str(i[2]).encode('ascii','ignore')
attrs['sshPublicKey'] = "ssh-rsa" + str(contrasena)
attrs['homeDirectory'] = ['/home/%s' % (str(usuario))]
attrs['loginShell'] = ['/bin/bash']
ldif = modlist.addModlist(attrs)
attrs ['ShadowLastChange'] = 0
uri.add(dn,ldif)
uidNumber = uidNumber + 1
uri.unbind_s()

#Envio un correo con los datos al nuevo usuario
from_address = 
str('[email protected]').encode('ascii','ignore')
passwr = 'correodeprueba234'
to_address = str(i[2]).encode('utf-8')
message = "Tu nuevo usuario es: " + usuario + '\n\n' + u"Tu contrasena 
: " + contrasena + '\n\n' + "Por favor ingresar haga click en el 
siguiente link: "
mime_message = MIMEText(message, "plain")
mime_message["From"] = from_address
mime_message["To"] = to_address
mime_message["Subject"] = "Nuevo registro de usuario"
smtp = SMTP('smtp.gmail.com:587')
smtp.ehlo()
smtp.starttls()
smtp.login(from_address,passwr)
smtp.sendmail(from_address, to_address, mime_message.as_string())
smtp.quit()
csvarchivo.close()
    
asked by Luis 03.12.2017 в 02:00
source

0 answers