-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchAllUsers.py
43 lines (29 loc) · 1.27 KB
/
searchAllUsers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from ldap3 import Server, Connection, ALL
import pprint
## Python replacement for SDI for verifying and manipulating LDAP and user data
# See: https://ldap3.readthedocs.io/en/latest/operations.html
## Iterator
ldap_host = 'ldap://<server>:<port>'
bind_user = '<bind user>'
bind_pwd = '<bind_pwd>'
basepoint = '<basepoint>'
# Not that much error handling yet, a connection fail will end all or skip entry
# Connect to server
server = Server(ldap_host, get_info=ALL)
conn = Connection(server, bind_user , bind_pwd, auto_bind=True)
# Non paged search
conn.search( basepoint, '(objectClass=*)', attributes= ['cn', 'givenName', 'mail'], paged_size= 2 )
for entry in conn.entries:
print(entry.entry_to_ldif())
# Doing a paged search seems to require a additional manual handling to handle paged_cookie and doing subsequent searches
searchParameters = { 'search_base': basepoint, 'search_filter': '(objectClass=*)', 'attributes': ['cn', 'givenName', 'mail', 'objectclass'], 'paged_size': 5 }
while True:
# Do search
conn.search(**searchParameters)
for entry in conn.entries:
print(entry.entry_to_ldif())
cookie = conn.result['controls']['1.2.840.113556.1.4.319']['value']['cookie']
if cookie:
searchParameters['paged_cookie'] = cookie
else:
break