Created
September 18, 2013 23:11
-
-
Save ligfx/6617050 to your computer and use it in GitHub Desktop.
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
# LDAP Auth | |
# coding=utf-8 | |
# pip install python-ldap | |
import ldap | |
from sys import stdin, stdout | |
stdout.write("Username: ") | |
USERNAME = stdin.readline().strip() | |
stdout.write("Password: ") | |
PASSWORD = stdin.readline().strip() | |
l = ldap.initialize("ldap://ldap.pomona.edu:389") | |
l.protocol_version = ldap.VERSION3 | |
try: | |
l.simple_bind_s(("%s@campus" % USERNAME).encode('utf-8'), PASSWORD.encode('utf-8')) | |
except ldap.LDAPError: | |
print "Could not log in!" | |
exit() | |
# And we're logged in! | |
# except ldap.LDAPError | |
ldap_result_id = l.search( | |
"OU=Users and Computers,OU=ZHOME,DC=campus,DC=pomona,DC=edu", | |
ldap.SCOPE_SUBTREE, | |
("(cn=%s)" % USERNAME), | |
None) | |
def sentinel(func, comp): | |
while True: | |
result = func() | |
if comp(result): break | |
yield result | |
results = filter( | |
lambda _: _[0] == ldap.RES_SEARCH_ENTRY, | |
sentinel( | |
lambda: l.result(ldap_result_id, 0), | |
lambda _: _[1] == []) | |
)[0][1][0][1] | |
print results['givenName'][0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment