Skip to content

Instantly share code, notes, and snippets.

@caseydunham
Created July 30, 2014 17:37
Show Gist options
  • Save caseydunham/d8b58408ab1594d13615 to your computer and use it in GitHub Desktop.
Save caseydunham/d8b58408ab1594d13615 to your computer and use it in GitHub Desktop.
Retrieve user information from AD via Python LDAP
import sys, ldap
# DN = [email protected], secret = password, un = username
DN, secret, un = sys.argv[1:4]
server = "ldap://server.com"
port = 389
base = "dc=example,dc=com"
scope = ldap.SCOPE_SUBTREE
filter = "(&(objectClass=user)(sAMAccountName=" + un + "))"
attrs = ["*"]
l = ldap.initialize(server)
l.protocol_version = 3
l.set_option(ldap.OPT_REFERRALS, 0)
print l.simple_bind_s(DN, secret)
r = l.search(base, scope, filter, attrs)
type, user = l.result(r, 60)
name, attrs = user[0]
if hasattr(attrs, 'has_key') and attrs.has_key('displayName'):
print attrs
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment