Skip to content

Instantly share code, notes, and snippets.

@morteza-mori
Forked from mortymacs/ad_test.py
Created May 22, 2018 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morteza-mori/8c917b4776fb0ea269615bb0cfc0452f to your computer and use it in GitHub Desktop.
Save morteza-mori/8c917b4776fb0ea269615bb0cfc0452f to your computer and use it in GitHub Desktop.
Retrieve user information from AD via Python LDAP
import sys, ldap
# DN = username@example.com, 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