Created
July 30, 2014 17:37
-
-
Save caseydunham/d8b58408ab1594d13615 to your computer and use it in GitHub Desktop.
Retrieve user information from AD via Python LDAP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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