Thank you but I dont have any knowledge of working with ldap so the "ldapsearch" hasn't helped.
I was able to simplify one of your examples so this 90% achieves what I need.
Although I find this is also listing the domain names.
# encoding: utf-8
# * bind_dn must have write privilege on LDAP server.
uri = 'ldap://127.0.0.1:389'
basedn = 'o=domains,dc=example,dc=com'
bind_dn = 'cn=Manager,dc=example,dc=com'
bind_pw = 'pass'
# Initialize LDAP connection.
conn = ldap.initialize(uri=uri, trace_level=0,)
# Get all mail users.
allUsers = conn.search_s(
# Print all email users separated by comma
for user in allUsers:
(dn, entry) = user
mail = entry['mail']
if len(mail) > 0:
print >> sys.stdout, "%s, " %(mail)
# Unbind connection.