7 class LdapDirectory(object):
9 self.con = ldap.open('ldap-too.mit.edu')
10 self.con.simple_bind_s("", "")
12 def ldap_lookup(self, username):
13 dn = "ou=users,ou=moira,dc=mit,dc=edu"
14 fields = ['eduPersonAffiliation', 'mitDirStudentYear']
15 userfilter = ldap.filter.filter_format('uid=%s', [username])
16 result = self.con.search_s(dn, ldap.SCOPE_SUBTREE, userfilter, fields)
19 print >>sys.stderr, "Unexpectedly received multiple results: %s" % (result, )
24 def classify(ldap_result):
26 if ldap_result == False:
28 elif ldap_result == {}:
31 primary = ldap_result['eduPersonAffiliation'][0]
32 secondary = ldap_result.get('mitDirStudentYear', ['-'])[0]
33 return primary, secondary
35 if __name__ == '__main__':
36 print >>sys.stderr, "Connecting to LDAP..."
37 directory = LdapDirectory()
38 print >>sys.stderr, "Starting lookups..."
39 for name in sys.stdin:
41 result = directory.ldap_lookup(name)
42 primary, secondary = classify(result)
43 print "%s\t%s\t%s" % (primary, secondary, name)