This script gives all the users data (eg. given output) but I want to fetch specific user's uid, uidnumber, mail, employeenumber. How do I do that?

Question:

What I should I do to get users uid number, mail, employeenumber?

 from ldap3 import Server, Connection
            # clear connection
            my_server = 'XXX'
            my_user = 'uid=idmsa,ou=People,ou=auth,o=csun'
            my_password = 'password'
            
            s0 = Server(my_server)
            c0 = Connection(s0, my_user, my_password)
            c0.bind()
            c0.search("o=csun", "(cn=*)")
            print(c0.entries)

OUTPUT

    DN: uid=aa22342,ou=People,ou=Auth,o=CSUN - STATUS: Read - READ TIME: 2021-06-24T10:27:10.169992
Asked By: Robin Malhotra

||

Answers:

You can pass in the list of attributes to be returned :

c0.search("o=csun", "(cn=*)", attributes=['uid', 'uidNumber', 'mail'])

And then you can iterate over the results with :

for entry in c0.response:
    print(entry['dn'])
    print(entry['attributes'])
    # ...
Answered By: EricLavault

You can specify on the search which attributes you want to be returned, default is none.

For example :

c0.search(search_base = 'o=csun',
     search_filter = '(cn=*)',
     search_scope = SUBTREE,
     attributes = ['uid', 'uidNumber', 'mail', 'employeeNumber'])

You can find all the parameters of the search function here :
https://ldap3.readthedocs.io/en/latest/searches.html

Answered By: Esteban