Skip to content

Instantly share code, notes, and snippets.

@hampelm
Created November 19, 2010 21:29
Show Gist options
  • Save hampelm/707223 to your computer and use it in GitHub Desktop.
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
'''
Matt Hampel <hampelm@umich.edu>
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,smith@umich.edu,Associate Professor, Health Behavior
John Smith,jsmith@umich.edu,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': ['hampelm@umich.edu'],
'postalAddress': ['Community Service Learning $ 1024 Hill Street $ Ann Arbor MI 48109'],
'onvacation': ['FALSE'],
'description': ["I manage ...snip..."],
'termstart': ['20100907000000Z'],
'krbName': ['hampelm@umich.edu'],
'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