SKP
SKP

Reputation: 161

How to get the user belongs to which group in python

I want to get the AD groups of user on the basis of active directory name. I have used ldap3 module but it didnt solve the problem of invalid credential although I have entered the correct one

 conn = Connection(Server('<AD server name>', port=389, use_ssl=False),
               auto_bind=AUTO_BIND_NO_TLS, user='<username>',
               password='<password>')

error: ldap3.core.exceptions.LDAPBindError: automatic bind not successful - invalidCredentials

Upvotes: 0

Views: 2178

Answers (1)

You can try using NTLM authentication.

This works for me:

from ldap3 import Server, Connection, NTLM

server = Server("in.domain.com")
with Connection(server, user="{}\\{}".format("MYDOMAIN", "your_username"), password="your_password", authentication=NTLM) as conn:
    conn.search("dc=in,dc=domain,dc=com", "(&(sAMAccountName={}))".format("user_you_want_to_find"),
                attributes=['memberOf'])
    entry = conn.entries

for group in entry[0]['memberOf']:
    print(group)

The script should print the Common Name of each group that user_you_want_to_find belongs to...

Regards.

Upvotes: 1

Related Questions