Skip to content

Instantly share code, notes, and snippets.

@deeso
Created June 3, 2021 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deeso/ca020d78154a1fc9edf3d24b888d7285 to your computer and use it in GitHub Desktop.
Save deeso/ca020d78154a1fc9edf3d24b888d7285 to your computer and use it in GitHub Desktop.
Searching Ldap with ldap3
import ldap3
password = ''
host_ip = ""
user_dn = "cn=Allen,OU=abc,OU=efgh,OU=zyx,DC=fqdg,DC=internal"
USERNAME = 'username'
DISPLAY_NAME = 'displayName'
MANAGER = 'manager'
NAME = 'name'
MAIL = 'mail'
TITLE = 'title'
MEMBER_OF = 'memberOf'
SUBJECT_ATTRIBUTES = [DISPLAY_NAME, TITLE, NAME, MAIL, MANAGER, MEMBER_OF]
server = ldap3.Server(host_ip, get_info=ldap3.ALL)
conn = ldap3.Connection(server,user_dn, password, auto_bind=True)
conn.bind()
ldap_filter = "(cn=*)"
base_dn = "OU=abc,OU=efgh,OU=zyx,DC=fqdg,DC=internal"
entry_generator = conn.extend.standard.paged_search(base_dn,
ldap_filter,
search_scope=ldap3.SUBTREE,
attributes=SUBJECT_ATTRIBUTES,
paged_size=200,
generator=True)
entries = [e for e in entry_generator]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment