1
1
Fork 0

added simple ldap connection script for parsing email addresses

This commit is contained in:
David 2012-05-21 18:22:01 +02:00
parent 0e8864fec0
commit d3018900e3
1 changed files with 49 additions and 0 deletions

49
ldap_connection.py Executable file
View File

@ -0,0 +1,49 @@
#!/usr/bin/python2 -B
""" ldap parser """
import ldap
ldap_server = "ldap://dc2.aec.at"
bind_dn = "david@aec.at"
bind_pass = "secret"
userbase = "ou=adm,dc=aec,dc=at"
keyword = "david"
def my_search(keyword):
result = instance.search_s(userbase, ldap.SCOPE_SUBTREE, ("cn=*"), ["mail", "proxyAddresses", "userAccountControl"])
return result
try:
instance = ldap.initialize(ldap_server)
bind = instance.simple_bind_s(bind_dn, bind_pass)
print "Successfully bound to server %s." %(ldap_server)
print "Searching..\n"
counter = 0
for item in my_search(keyword):
if item[1].has_key("userAccountControl"):
if item[1]["userAccountControl"] == ["512"] or item[1]["userAccountControl"] == ["66048"]:
if item[1].has_key("proxyAddresses"):
addresslist = item[1]["proxyAddresses"]
for address in addresslist:
if "SMTP:" in address or "smtp:" in address:
print address[5:].lower() + " OK"
counter += 1
print "objects: %s" %(counter)
except ldap.LDAPError, error_message:
print "Couldn't connect to server %s." %(ldap_server)
try:
instance.unbind()
print "Successfully unbound from server %s." %(ldap_server)
except ldap.LDAPError, error_message:
print "Couldn't unbind from server %s." %(ldap_server)
#end of file