#!/usr/bin/env python from ldap3 import Connection, Server, ANONYMOUS, SIMPLE, SYNC, ASYNC from getpass import getpass import configparser config = configparser.ConfigParser() config.read(cwd + '/config.ini') serverDNS = config['LDAP SERVER']['server'] serverPort = config['LDAP SERVER']['port'] serverSSL = config['LDAP SERVER']['ssl'] bindAccount = config['Bind Account']['username'] bindPassword = config['Bind Account']['password'] server = Server(serverDNS, port=serverPort, use_ssl=serverSSL) username = input("Username: ") password = getpass("Password: ") #Create a connection object, and bind with the given DN and password. try: conn = Connection(server, username, password, auto_bind=True) print('LDAP Bind Successful.') # Perform a search for a pre-defined criteria. # Mention the search filter / filter type and attributes. conn.search('CN=Users,dc=home,dc=johnhgaunt,dc=com', '(&(mail=*)(!(userAccountControl:1.2.840.113556.1.4.803:=2))(memberOf=CN=Seafile,CN=Users,DC=home,DC=johnhgaunt,DC=com))') # Print the resulting entries. for entry in conn.entries: print(entry) except core.exceptions.LDAPBindError as e: # If the LDAP bind failed for reasons such as authentication failure. print('LDAP Bind Failed: ', e)