pythonactive-directoryldapldap3

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?


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

Solution

  • 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'])
        # ...