1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| #!/usr/bin/python
# -*- coding:utf-8 -*-
############################
# Variables #
############################
_LDAP_SERVER_FTP = "ftp"
_LDAP_USERS_BASE_DN_FTP = "ou=Users,dc=exemple,dc=fr"
_LDAP_DOMAIN_BASE_DN_FTP = "dc=exemple,dc=fr"
_LDAP_CRED_LOGIN_FTP = "cn=admin,dc=exemple,dc=fr"
_LDAP_CRED_PASSWD_FTP = "secret"
#############################
# Modules #
#############################
import ldap
import ldap.modlist as modlist
############################
# Main #
############################
try:
_ldap_session = ldap.initialize("ldap://%s/" % (_LDAP_SERVER_FTP))
_ldap_session.protocol_version = ldap.VERSION3
_ldap_session.simple_bind_s(_LDAP_CRED_LOGIN_FTP, _LDAP_CRED_PASSWD_FTP)
except ldap.LDAPError, e:
print "ERREUR: Impossible de se connecter au serveur LDAP: ", e
sys.exit(0)
try:
dn="cn=usertest,ou=Users,dc=st-ouen,dc=fr"
# Dictionnaire des attributs souhaites
attrs = {}
attrs['objectclass'] = ['top','inetOrgPerson','posixAccount']
attrs['cn'] = 'usertest'
attrs['userPassword'] = 'secret'
# Convertion du distionnaire
ldif = modlist.addModlist(attrs)
# Envoie vers le serveur
_LDAP_SERVER_FTP.add_s(dn,ldif)
except ldap.LDAPError, e:
print e
sys.exit(0) |
Partager