ldap3 python search members of a group and retrieve their sAMAcountName (NT Username)

Question:

I retrieve members of a group and their sAMAccountName (NT Accounts)
I run the following code takes from:
ldap3 python search members of a group and retrieve their sAMAcountName (Active Directory)

I’m getting user name as output but after few I’m getting the error bellow:

ldap_conn.search(search_base='DC=DOMAIN,DC=com',search_filter=f'(distinguishedName={member})',attributes=['sAMAccountName'])
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/ldap3/core/connection.py", line 838, in search
    request = search_operation(search_base,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/ldap3/operation/search.py", line 371, in search_operation
    request['filter'] = compile_filter(parse_filter(search_filter, schema, auto_escape, auto_encode, validator, check_names).elements[0])  # parse the searchFilter string and compile it starting from the root node
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/ldap3/operation/search.py", line 214, in parse_filter
    raise LDAPInvalidFilterError('malformed filter')
ldap3.core.exceptions.LDAPInvalidFilterError: malformed filter

I noticed, it failed on users who have ( ) , ? , # charters in User’s Display Name(distinguishedName) , how can I filter out all non regular charters (A-Z) ?

base = "CN=mygroup,OU=Security Group,OU=Resources,OU=Global,DC=Domain,DC=com"
ldap_conn.search(search_base = base,search_filter = '(objectClass=group)',search_scope='SUBTREE',attributes = ['member'])

for entry in ldap_conn.entries:
    for member in entry.member.values:
        ldap_conn.search(search_base='OU=Global,DC=Domain,DC=com',search_filter=f'(distinguishedName={member})',attributes=['sAMAccountName'])
        user_sAMAccountName = ldap_conn.entries[0].sAMAccountName.values
        print(user_sAMAccountName)
Asked By: shlco

||

Answers:

The error message "IndexError: list index out of range" occurs because the ldap_conn.entries list is empty, meaning that no entries were returned by the first search query. This could be due to a few reasons, such as an incorrect search base or search filter.

Here are a few steps you can take to troubleshoot the issue:

Verify that the search base and filter are correct. Check that the base and filter match the location and name of the group you are trying to retrieve members from.

Make sure that the LDAP connection is established successfully before making any search queries. You can use the bind() method to authenticate and establish a connection before running the search queries.

Try printing out the entries returned by the first search query to see if there are any results. You can do this by adding a print statement after the first search query:

for entry in ldap_conn.entries:
    print(entry)

If there are no results, it could mean that there are no entries that match the search filter.

Check that the attribute ‘sAMAccountName’ exists for the LDAP entries that are being returned. You can do this by adding a print statement after the second search query:

for entry in ldap_conn.entries:
    print(entry.sAMAccountName)

This will print the values of the ‘sAMAccountName’ attribute for each LDAP entry. If the attribute does not exist, you will get an error message similar to the one you posted.

I hope this helps you troubleshoot the issue. Let me know if you have any further questions!

Answered By: MidasN74