- this is quick and dirty. it assumes cn is monovalued which may not be true in your DIT
- I assume you just wanted a quick script for a oneshot. if you want a script that you can regularly run to "fix" your database, you should take the time to properly write one with perl, python or whatever
I usually use bash+awk for quick & dirty, but I took the time to rewrite the script in python using the python-ldap library:
LDAP_URL = 'ldap://server'
USER_DN = 'cn=writer,dc=domain,dc=tld'
CREDENTIALS = 'supersecret'
BASE_DN = 'dc=domain,dc=tld'
FILTER = '(&(ou:dn:=people)(!(ou=system))(!(displayName=*)))'
import ldap
l = ldap.initialize(LDAP_URL)
l.bind_s(USER_DN, CREDENTIALS, ldap.AUTH_SIMPLE)
result = l.search_s(BASE_DN, ldap.SCOPE_SUBTREE, FILTER, ['cn'])
for dn, entry in result:
l.modify_s(dn, (ldap.MOD_REPLACE, 'displayName', entry['cn'][0]))
l.unbind_s()