def get_ldap_user(member_cn, user, passwrd):
'''
Get an LDAP user and return the SAMAccountName
'''
bindDN = str(user) + "@unt.ad.unt.edu"
bindPass = passwrd
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_ALLOW)
conn = ldap.initialize('ldaps://unt.ad.unt.edu')
conn.protocol_version = 3
conn.set_option(ldap.OPT_REFERRALS, 0)
try:
result = conn.simple_bind_s(bindDN, bindPass)
except ldap.INVALID_CREDENTIALS:
result = "Invalid credentials for %s" % user
sys.exit()
ldap_query = '(|(displayName=' + member_cn + ')(cn='+ member_cn + ')(name=' + member_cn + '))'
ldap_info = conn.search_s('DC=unt,DC=ad,DC=unt,DC=edu', ldap.SCOPE_SUBTREE, filterstr=ldap_query)
sAMAccountName = str(ldap_info[0][1]['sAMAccountName']).replace("[b'", "").replace("']","")
return sAMAccountName