Created
November 9, 2011 17:21
-
-
Save limed/1352157 to your computer and use it in GitHub Desktop.
Playing with paged queries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
import re | |
import ldap | |
from ldap.controls import SimplePagedResultsControl | |
server = "" | |
basedn = "" | |
rootdn = "" | |
rootpw = "" | |
attr = ['mail','proxyAddresses','targetAddress','cn','homemdb','objectclass'] | |
pagesize = 1000 | |
def connect_ldap(ldapserver): | |
try: | |
print "Connecting to " + ldapserver | |
conn = ldap.initialize(ldapserver) | |
except ldap.LDAPError, e: | |
sys.stderr.write(str(e) + '\n') | |
sys.exit(1) | |
return conn | |
def bind_ldap(conn, who, password): | |
try: | |
conn.simple_bind_s(who,password) | |
except ldap.LDAPError, e: | |
sys.stderr.write(str(e) + '\n') | |
if __name__ == '__main__': | |
ldap.set_option(ldap.OPT_REFERRALS, 0) | |
conn = connect_ldap(server) | |
#fh = open('ldap.output','w') | |
print "Trying to bind to " + server | |
bind_ldap(conn, rootdn, rootpw) | |
print "Bind successful" | |
lc = SimplePagedResultsControl( | |
ldap.LDAP_CONTROL_PAGE_OID,True,(pagesize,'') | |
) | |
pages = 0 | |
filter = '(&(proxyAddresses=*@oregonstate.edu))' | |
result_id = conn.search_ext(basedn, ldap.SCOPE_SUBTREE, filter, attr, serverctrls=[lc]) | |
pages = 0 | |
while True: | |
pages += 1 | |
print "Getting page %d" % (pages,) | |
rtype, rdata, rmsgid, serverctrls = conn.result3(result_id) | |
#print "%d results" % len(rdata) | |
#print rdata | |
#str_rdata = str(rdata) | |
#fh.write(str_rdata) | |
# Stuff we care about | |
attributes = ('user', 'group', 'msExchDynamicDistributionList', 'publicFolder') | |
for dn, entry in rdata: | |
for entries in entry['objectClass']: | |
for i in attributes: | |
if i in entries: | |
print "DN - " + dn + "\tProxy address - " + str(entry['proxyAddresses']) | |
cookie = None | |
for serverctrl in serverctrls: | |
if serverctrl.controlType == ldap.LDAP_CONTROL_PAGE_OID: | |
unused_est, cookie = serverctrl.controlValue | |
if cookie: | |
lc.controlValue = (pagesize, cookie) | |
result_id = conn.search_ext(basedn, ldap.SCOPE_SUBTREE, filter, attr, serverctrls=[lc]) | |
break | |
if not cookie: | |
break | |
#fh.close() | |
conn.unbind |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment