Created
November 19, 2010 21:29
-
-
Save hampelm/707223 to your computer and use it in GitHub Desktop.
A small script for getting information in bulk out of the U-M Directory
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
''' | |
Matt Hampel <[email protected]> | |
A small script for getting information in bulk out of the U-M Directory. | |
Requires: python-ldap (easy_install python-ldap) | |
Use: python scripy.py infile.csv outfile.csv | |
Where infile.csv is a text file with one name per line. | |
Results: Gives you outfile.csv and associated email address, one per line. | |
NOTE: If there are several possible matches for a name, the script will print | |
each, with a hint for how to disambiguate. For example: | |
John Smith,[email protected],Associate Professor, Health Behavior | |
John Smith,[email protected],Ross School of Business | |
For reference, here's what the directory returns, so you can add or remove | |
fields at will: | |
{ | |
'cn': ['Matthew Schuhwerk Hampel', 'Matthew S Hampel', 'Matthew Hampel', 'Matt Hampel'], | |
'objectClass': ['posixAccount', 'top', 'person', 'organizationalPerson', | |
'umichPerson', 'inetOrgPerson'], | |
'uidNumber': ['231932'], | |
'campus': ['A'], | |
'uid': ['hampelm'], | |
'title': ['Technology Project Manager', 'Student, Undergraduate L S & A'], | |
'loginShell': ['/bin/csh'], | |
'mail': ['[email protected]'], | |
'postalAddress': ['Community Service Learning $ 1024 Hill Street $ Ann Arbor MI 48109'], | |
'onvacation': ['FALSE'], | |
'description': ["I manage ...snip..."], | |
'termstart': ['20100907000000Z'], | |
'krbName': ['[email protected]'], | |
'drink': ['Apple cider'], | |
'gidNumber': ['10'], | |
'RealtimeBlockList': ['TRUE'], | |
'classstanding': ['Senior'], | |
'telephoneNumber': ['+1 734 647 9423'], | |
'term': ['1810'], | |
'displayName': ['Matthew Schuhwerk Hampel'], | |
'labeledURI': ['http://matth.org', 'http://arborwiki.org ArborWiki'], | |
'mobile': ['+1 734 846 5010'], | |
'registrationstatus': ['Withdrawn'], | |
'sn': ['Hampel'], | |
'homeDirectory': ['/users/hampelm'], | |
'ou': ['Undergraduate L S & A - Student', 'College of Lit, Science & Arts | |
- Faculty and Staff', 'LSA UG: Residential College - Faculty and Staff', | |
'Social Science BA - Student', 'Alumni', | |
'Minor -Computer Science BA - Student', | |
'VP for Student Affairs - Faculty and Staff', | |
'Community Service Learning - Faculty and Staff'] | |
} | |
''' | |
import csv | |
import ldap | |
import sys | |
f = sys.argv[1] | |
f = open(f) | |
writer = csv.writer(open(sys.argv[2], 'wt')) | |
writer.writerow(['Name', 'Uniquename', 'Disambiguation details']) | |
server = "ldap://ldap.itd.umich.edu" | |
l = ldap.initialize(server) | |
for line in f: | |
line = line.strip() # cut out whitespace | |
# You can change 'cn' to any of the fields the directory to search | |
# different attributes: | |
a = l.search_s('ou=People,dc=umich,dc=edu', ldap.SCOPE_SUBTREE, 'cn=' + line) | |
for elt in a: | |
elt = elt[1] | |
uniquename = elt['uid'][0] | |
email = uniquename + '@umich.edu' | |
title = elt['title'][0] | |
if len(a) == 1: | |
writer.writerow([line, email]) | |
else: | |
writer.writerow([line, email, title]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment