Last active
September 10, 2020 16:29
-
-
Save starzia/ede9182948fc9238b3c8998190a1ab02 to your computer and use it in GitHub Desktop.
Northwestern netid lookup script using LDAP
This file contains hidden or 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
# reads netids from stdin and prints email addresses to stdout | |
# see http://ldap3.readthedocs.io/tutorial_searches.html | |
# and https://www.it.northwestern.edu/bin/docs/CentralAuthenticationServicesThroughLDAP.pdf | |
# | |
# prereq: pip install ldap3 | |
# usage: echo [email protected] | python netid_lookup.py | |
from ldap3 import Server, Connection, ALL, ALL_ATTRIBUTES | |
import sys | |
def connect(): | |
server = Server(host='directory.northwestern.edu', get_info=ALL) | |
return Connection(server, auto_bind=True) | |
def lookup_netid(conn, email): | |
conn.search(search_base='dc=northwestern,dc=edu', search_filter='(mail=%s)' % email, | |
attributes=ALL_ATTRIBUTES) | |
return conn.entries[0].uid if len(conn.entries) else None | |
def main(): | |
conn = connect() | |
for netid in sys.stdin: | |
netid = lookup_netid(conn, netid) | |
print(netid if netid else "") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment