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.
#!/usr/bin/env python
# encoding: utf-8
import sys
import ldap
# Note:
# * 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,)
conn.bind_s(bind_dn, bind_pw)
# Get all mail users.
allUsers = conn.search_s(
basedn,
ldap.SCOPE_SUBTREE,
"(objectClass=mailUser)",
['mail'],
)
# Print all email users separated by comma
for user in allUsers:
(dn, entry) = user
mail = entry['mail'][0]
if len(mail) > 0:
print >> sys.stdout, "%s, " %(mail)
# Unbind connection.
conn.unbind()