Python‎ > ‎

Recursive user lookup from files

posted Sep 19, 2014, 4:34 PM by Chris G   [ updated Sep 19, 2014, 4:36 PM ]
This script was used to generate an LDAP LDIF file containing only the detailed information for users who were members of a specific group. The file "users.ldif" contains the detailed information about ALL users in the LDAP. The file "group.ldif" contains a list (no details) of all users who are members of the group of interest.

The structure of the users.ldif file is as follows:
.
version: 1

dn: ou=Users,dc=myorg,dc=com
objectClass: organizationalUnit
objectClass: top
ou: Users
description: Users organizational unit

dn: uid=user1,ou=Users,dc= myorg,dc=com
objectClass: person objectClass: organizationalPerson objectClass: inetOrgPerson objectClass: top cn: User sn: Test mail: o: MYORG telephoneNumber: title: uid: user1 userPassword:: RANDOM Q== dn: uid=user2,ou=Users,dc= myorg,dc=com
objectClass: person objectClass: organizationalPerson objectClass: inetOrgPerson objectClass: top cn: User sn: Test mail: o: MYORG telephoneNumber: title: uid: user2 userPassword:: RANDOM Q== ...


The structure of the group.ldif file is as follows:
.
version: 1

dn: cn=groups,ou=roles,dc=myorg,dc=com
objectClass: groupOfUniqueNames
objectClass: top
cn: MY_GROUP
description: Description of the Group
o: Group
uniqueMember: uid=admin,ou=Users,dc=myorg,dc=com
uniqueMember: uid=user1,ou=Users,dc=myorg,dc=com
uniqueMember: uid=user2,ou=Users,dc=myorg,dc=com
uniqueMember: uid=user3,ou=Users,dc=myorg,dc=com
uniqueMember: uid=user4,ou=Users,dc=myorg,dc=com
uniqueMember: uid=user5,ou=Users,dc=myorg,dc=com
uniqueMember: uid=user6,ou=Users,dc=myorg,dc=com

...


And the Python script to read both files and create a file containing the detailed information only for user who are members of the group:
.
f = open("users.ldif")
lines = f.readlines()
f.close()

#Open the output file in Write mode
fw = open('group_user_details.txt', 'w')


#open the group file and iterate through each user's UID
#Some reformating is needed to match the uid since the format is different in each file
f = open("group.ldif")
for line in iter(f):
    if line[:14] == 'uniqueMember: ':
        print 'dn: u' + line[15:]
        searchName = ('dn: u' + line[15:]).strip()
        print str(searchName[4:])
        i = 0
        for index,value in enumerate(lines):
            if value[:-1] == searchName:
                i = index
                #print i
                break

        if i > 0:
            for j in range(i, i+14):
                tempText = lines[j].strip()
                if tempText == 'Q==':
                    break
                if tempText[:13] != 'userPassword:':
                    print tempText
                    fw.write(tempText + '\n')
            print '----------------------'
            fw.write('\n')
        else:
            print '***Not found'

f.close()

fw.close()




There might be better ways to handle this, but this actually works very well and is pretty fast.
Comments