33 lines
1.2 KiB
Python
33 lines
1.2 KiB
Python
#!/usr/bin/env python
|
|
|
|
from ldap3 import Connection, Server, ANONYMOUS, SIMPLE, SYNC, ASYNC
|
|
from getpass import getpass
|
|
import configparser
|
|
|
|
# import the config file
|
|
config = configparser.ConfigParser()
|
|
config.read(cwd + '/config.ini')
|
|
|
|
serverDNS = config['LDAP SERVER']['server']
|
|
serverPort = config['LDAP SERVER']['port']
|
|
serverSSL = config['LDAP SERVER'].getboolean('ssl')
|
|
|
|
bindAccount = config['Bind Account']['username']
|
|
bindPassword = config['Bind Account']['password']
|
|
|
|
# setup the server
|
|
server = Server(serverDNS, port=serverPort, use_ssl=serverSSL)
|
|
|
|
#Create a connection object, and bind with the given DN and password.
|
|
try:
|
|
conn = Connection(server, bindAccount, bindPassword, 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) |