Skip to content

Instantly share code, notes, and snippets.

@limed
Created November 11, 2011 01:01
Show Gist options
  • Save limed/1356804 to your computer and use it in GitHub Desktop.
Save limed/1356804 to your computer and use it in GitHub Desktop.
Normal non paging ldap search
#!/usr/bin/python
import sys
import re
import ldap
server = ""
basedn = ""
rootdn = ""
rootpw = ""
attr = ['mail','proxyAddresses','targetAddress','cn','homemdb','objectclass']
alphabet = 'abcdefghijklmnopqrstuvwxyz'
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')
def extract_correct_entries(array_name, retvals):
attributes = ('user', 'group', 'msExchDynamicDistributionList', 'publicFolder')
for entries in array_name:
for i in attributes:
if i in entries:
return retvals
if __name__ == '__main__':
ldap.set_option(ldap.OPT_REFERRALS, 0)
conn = connect_ldap(server)
orst_regex = re.compile('\@oregonstate.edu', re.IGNORECASE)
print "Trying to bind to " + server
bind_ldap(conn, rootdn, rootpw)
print "Bind successful"
for i in alphabet:
print "Search - " + i
filter = '(&(proxyAddresses=*@oregonstate.edu)(cn=*' + i + '*))'
result_id = conn.search_s(basedn, ldap.SCOPE_SUBTREE, filter, attr)
for dn, entry in result_id:
print dn
conn.unbind
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment