Python Forum

Full Version: LDAP3 not return a list correctly?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I am using LDAP3 to retrieve every computer in my domain.

from ldap3 import Server, Connection, ALL
ad_computer_list = []


# Procedure to get AD computers
def get_ad_computers():
    search_domain = ('dc=uk,dc=co,dc=uk')
    search_user = ('CN=Lady DAP,OU=mc_u_service_,OU=mc,DC=uk,DC=co,DC=uk')
    search_password = ('password')
    # Set connection to LDAP Server
    ldap_server = Server('10.0.1.2', port = 636, use_ssl = True, get_info=ALL)
    server_query = Connection(ldap_server, search_user, search_password, auto_bind=True)
    #Check to see if you can connect on 636
    if server_query.bind() == True:
        #print(ldap_server.info)
        server_query.search(search_domain, '(objectclass=computer)')
        #server_query.search(search_domain, '(&(objectclass=computer)(name=*))')
        computer_names = server_query.entries
        print(server_query.entries)
    else:
        server_query.search(search_domain, '(objectclass=computer)')
        computer_name = server_query.entries
        print("389: ",computer_name)
It finds all the machines for me, but when I debug the list, it would seem it is not a proper list. It doesn't have a len value. I just want the computer name to compare to another list.

The error is

Unable to display children:Error resolving variables Traceback (most recent call last):
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydevd_bundle/pydevd_comm.py", line 1208, in do_it
xml.write(pydevd_xml.var_to_xml(val, k, evaluate_full_value=evaluate_full_value))
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydevd_bundle/pydevd_xml.py", line 369, in var_to_xml
elif hasattr(v, "__len__") and not is_string(v):
File "/Users/sm/Documents/_personal files/Folder/Python/GitHub/venv/lib/python3.8/site-packages/ldap3/abstract/entry.py", line 196, in __getattr__
raise LDAPCursorError(error_message)
ldap3.core.exceptions.LDAPCursorError: attribute '__len__' not found

What can I do to avoid this?

Best wishes

Michael