Skip to content

Instantly share code, notes, and snippets.

@starzia
Last active September 10, 2020 16:29
Show Gist options
  • Save starzia/ede9182948fc9238b3c8998190a1ab02 to your computer and use it in GitHub Desktop.
Save starzia/ede9182948fc9238b3c8998190a1ab02 to your computer and use it in GitHub Desktop.
Northwestern netid lookup script using LDAP
# 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 abc2020@u.northwestern.edu | 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