Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
a python script to hook up mutt to your company's horrible Microsoft Exchange address book
#!/usr/bin/python
'''This is a python port of http://www.bsdconsulting.no/tools/mutt-ldap.pl
'''
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(
basedn,
ldap.SCOPE_SUBTREE,
filterstr="(|(cn=*%(query)s*)(rdn=*%(query)s*)(uid=*%(query)s*)(mail=*%(query)s*))" % {'query': query},
attrlist=['mail','cn'])
for dn, entry in result:
try:
print '%s\t"%s" <%s>\tFrom Exchange LDAP database' % (entry['mail'][0], entry['cn'][0], entry['mail'][0])
except KeyError:
pass
l.unbind_s()
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