Skip to content

Instantly share code, notes, and snippets.

@pads
Created July 24, 2013 15:38
Show Gist options
  • Save pads/6071727 to your computer and use it in GitHub Desktop.
Save pads/6071727 to your computer and use it in GitHub Desktop.
Print a list of users not found in an LDAP directory, given a file where each line is a user ID that would map to a valid CN in the LDAP directory.
import ldap
ldap_host = '<host>'
ldap_port = '389'
base_dn = '<ou=something,ou=com>'
username = '<username>'
password = '<password>'
filename = '<file_with_list_of_user_ids>'
ldap_instance = ldap.initialize('ldap://%s:%s' % (ldap_host, ldap_port))
ldap_instance.simple_bind_s('cn=%s,%s' % (username, base_dn), password);
user_id_list = open(filename, 'r')
for line in user_id_list:
user_id = line.rstrip('\n')
results = ldap_instance.search(base_dn, ldap.SCOPE_SUBTREE, 'cn=%s' % user_id, ['cn'])
result_type, result_data = ldap_instance.result(results, 0)
if len(result_data) == 0:
print user_id
user_id_list.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment