How to read the contents of an active directory using python-ldap?

My script looks like this:

import ldap, sys server = 'ldap://my_server' l = ldap.initialize(server) dn=" myname@mydomain " pw = "password" l.simple_bind_s(dn,pw) ldap.set_option(ldap.OPT_REFERRALS,0) print "valid" 

I am using Python 2.7 on windows.

Is there any method to read or retrieve the contents of the active directory?

+4
source share
2 answers

You can do quite a lot with win32com.client as well (I had problems finding documentation). For example, I needed to allow a user’s email, knowing his ADS_NAME_TYPE_NT4 formatted name ( doman\jonjoe ).

First of all, you need to convert it to the format ADS_NAME_TYPE_1779 ( CN=Jeff Smith,CN=users,DC=Fabrikam,DC=com ):

 name_resolver = win32com.client.Dispatch(dispatch='NameTranslate') name_resolver.Set(3, 'domain\\jonjoe') ldap_query = 'LDAP://{}'.format(name_resolver.Get(1)) 

After that, you can simply call GetObject() :

 ldap = win32com.client.GetObject(ldap_query) print(ldap.Get('mail')) 

Tested with Python 3.2.5

+4
source

You really need to read the python-ldap documentation http://www.python-ldap.org/docs.shtml

 You have a connection in your variable l, then you can do this. l.con_search_s('dc=your,dc=base,dc=dit', ldap.SCOPE_SUBTREE, 'uid=*', ['uid', 'uidnumber']) The above code, goint to search in to all the uid entrys, for if entry, is going to get the uid and the uidnumbre attributes. 
0
source

Source: https://habr.com/ru/post/1446374/


All Articles