this is the final code that I used for modifying existing entries.
from ldap3 import Server, Connection
from django.conf import settings
import ldap3
import sys
# define the server
s = ldap3.Server(settings.AUTH_LDAP_SERVER)
# define the connection
c = ldap3.Connection(s, user=settings.LDAP_ADMIN_DN,
password=settings.LDAP_ADMIN_PASSWORD,
raise_exceptions=True)
c.bind()
# uid to search and modify
uid_number_start=10053
uid_number_end=10400
x = range(uid_number_start, uid_number_end, 1)
for uid_number in x:
print("%s" % uid_number)
result = c.search(
search_base=settings.LDAP_CUSTOMER_DN,
search_filter='(&(objectClass=inetOrgPerson)(objectClass=posixAccount)'
'(objectClass=top)(uidNumber={uidNumber}))'.format(uidNumber=uid_number),
attributes=['uid', 'mail']
)
entries = c.entries
if len(entries)>0:
print(str(entries[0].mail) + " --- " + str(entries[0].entry_dn))
if entries[0].entry_dn.startswith("uid=b'"):
uid = entries[0].uid[0]
dn = entries[0].entry_dn
new_superior = dn[dn.index(",")+1:]
print("dn=%s, new_superior=%s, uid=%s" % (dn, new_superior, uid))
c.modify_dn(entries[0].entry_dn, "uid={uid}".format(uid=uid),True, new_superior)
print(c.result)
else:
print("Entry DN %s does not start with b'" % entries[0].entry_dn)
else:
print("Could not find an entry for uid_number=%s" % uid_number)
print("*******")
# close the connection
c.unbind()