Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
a python script to hook up mutt to your company's horrible Microsoft Exchange address book
'''This is a python port of
import ldap
# To do:
# - encode search string as UTF-8
# - decode cn from UTF-8
# - sanity check input search string
# - get configuration from external file
ldapserver = "..."
domain = "..."
username = "..."
password = "..."
company = "..."
basedn = "ou=%(company)s,dc=%(company)s,dc=int" % {'company': company}
def search(query):
l = ldap.initialize('ldap://%s' % ldapserver)
l.simple_bind_s(r"%s\%s" % (domain, username), password)
result = l.search_s(
filterstr="(|(cn=*%(query)s*)(rdn=*%(query)s*)(uid=*%(query)s*)(mail=*%(query)s*))" % {'query': query},
for dn, entry in result:
print '%s\t"%s" <%s>\tFrom Exchange LDAP database' % (entry['mail'][0], entry['cn'][0], entry['mail'][0])
except KeyError:
if __name__ == '__main__':
import sys
raise SystemExit(search(sys.argv[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment