LDAP

Lightweight Directory Access Protocol

 

LDAP3 for Python

By Dr. Louay Chebib

 

 

 

 

LDAP Tool

LDAP

Sample Code -Access server using an anonymous the connection

from ldap3 import Server, Connection, ALL, SUBTREE

# define the server using its full active domain name (ldap_server_name.ND.ad.SITE.com):
# - ldap_server_name is an accesable LDAP server
# - ND is Internal Network domain
# - ad is active domain
# - SITE.com is the internet domain
s = Server('ldap_server_name.ND.ad.SITE.com', get_info=ALL)  # define an LDAP server

# define an anonymous the connection
conn = Connection(s)
conn.bind()
#display info
print(s.info)
print(conn)

LDAP

Sample Code -Access server to lookup user infromation

from ldap3 import Server, Connection, ALL, SUBTREE

# define the server using its full active domain name (ldap_server_name.ND.ad.SITE.com):
s = Server('ldap_server_name.ND.ad.SITE.com', get_info=ALL)  # define an LDAP server

# define the connection & bind using userID being checked
c = Connection(s, user='ND\\userID', password='secret')
# perform the Bind operation
if not c.bind():
    print('error in bind', c.result)

# get the field names for the attributes using the LDAP Admin Tool

c.search(search_base = 'DC=ND,DC=ad,DC=SITE,DC=com',
             search_filter =  '(cn=userID)',
             search_scope = SUBTREE,
             attributes = ['cn', 'givenName', 'displayName', 'mail',
                   'telephoneNumber', 'distinguishedName', 'memberOf'
])
print(c.entries)
print(c.response[0])
print(c.response[0]['attributes']['cn'])
print(c.response[0]['attributes']['mail'])
print(c.response[0]['attributes']['displayName'])
print(c.response[0]['attributes']['telephoneNumber'])
for x in c.response[0]['attributes']['memberOf']:
    print(x)

LDAP3 Demo

By Dr. L. Chebib

LDAP3 Demo

LDAP (Lightweight Directory Access Protocol) LDAP3 for python

  • 1,428