Python LDAP Search

I read about how to look for LDAP servers using Python, but I got stuck for hours and I'm not sure why. This is my first experience using this API.

This is how I open the connection and try to search:

aims_server = '#####.com' base_dn = 'cn=EMPLOYEES,cn=portal,cn=Groups,dc=Company,dc=com' username = 'cn=admin,cn=users,dc=Company,dc=com' password='#####' directory=ldap.open(aims_server) directory.simple_bind_s(username, password) #retrieve the current members from group old = {'uniquemember':attr['uniquemember']} 

Then I intentionally break the code, so I can use the debugger and search using this:

 >>> searchFilter = "cn=*" >>> directory.search_s(base_dn,ldap.SCOPE_SUBTREE,searchFilter, retrieveAttributes) 

Results:

 [('cn=EMPLOYEES,cn=portal,cn=groups,dc=Company,dc=com', {'displayname': ['Employees'], 'description': ['Members of this group are employees. '], 'objectclass': ['top', 'groupOfUniqueNames', 'orclGroup'], 'orclisvisible': ['true'], 'owner': ['cn=portal_admin ,cn=users,dc=Company,dc=com', 'cn=portal,cn=users, dc=Company,dc=com'], 'uniquemember': ['cn=alan,cn=users,dc=Company,dc=com', 'cn=alan_r,cn=users,dc=Company,dc=com', .... 

If I have the filter "cn=*" , it will return the dictionary above, but if I actually put something in searchFilter , it will not bring any results.

Does anyone have any ideas? I wonder if I'm not looking deep enough in directories?

EDIT

The best I can understand is to change the settings:

 searchFilter = "cn=*" retrieveAttributes = ["uniquemember"] 

Then:

 (cn, attr) = searcher.pop() 

Return:

 {'uniquemember': ['cn=alan_t,cn=users,dc=company,dc=com','cn=alan_r,cn=users,dc=company.... 

It seems that he is trying to find a level too high, how would I go down to another level to look for unique members?

I just want to find their names!

+4
source share
2 answers

I finally did this and it took me only 5 hours.

Every time I messed around with the configuration, I found out a little more, but I basically had to try each combination to make it work.

Turns out I was probably too specific with base_dn, so I changed it to a higher level

 base_dn = 'cn=users,dc=company,dc=com' 

Then I realized that I could not find anyone below uniquemember , so there had to be an attribute that I returned

 retrieveAttributes = ["uniquemember"] 

So the filter works

 searchFilter = "cn=aaron*" 

Then he will return:

 [(' cn=Aaron_A@company.com ,cn=Users,dc=company,dc=com', {})] 

Although at the end it contains an empty object, it still gives me the result I'm looking for.

I hope this helps someone else when they are new to LDAP

+9
source

Instead...

 directory=ldap.open(aims_server) 

I used...

 directory=ldap.initialize(aims_server) 

Also, if you have ...

 searchFilter = "cn=alan" retrieveAttributes = ['cn'] results = directory.search_s( ... ) print results 

he still will not give you what you need?

0
source

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


All Articles