PyLDAP is the Python LDAP API bindings which provides strong interface for python programs to access Directory Services.
I started off with PyLDAP today and thought let me help my friends too
and thus this entry to my blog
I have written a small LDAP class as a python module and a main program which creates the ldap class object and displays the current OUs and Users associated with the OUs.
First let me start with the main.py file (Please bear with the indentations as I am not able to bring the indentation here in wordpress)
- import ldap
import udmsldap
import stringdef main():
ldms_ldap= udmsldap.UdmsLDAP(“udms-server.udms.com”,”cn=admin,dc=udms,dc=com”,”genius”)result_ous=ldms_ldap.get_ous()
result_users=ldms_ldap.get_users()parse_ous(result_ous)print(” “)
parse_users(result_users)
ldms_ldap.add_entry()
def parse_ous(result_ous):
print “Number of Organization Units : ” + str(len(result_ous))
for i in range(len(result_ous)):
for entry in result_ous[i]:
print entry[0].split(“,”)[0]
def parse_users(result_users):
print “Number of Users : ” + str(len(result_users))
print “Users” + ” –> ” + “OU”
print “*************”
for i in range(len(result_users)):
for entry in result_users[i]:
print entry[0].split(“,”)[0].split(“=”)[1] + ” –> ” + entry[0].split(“,”)[1].split(“=”)[1]
if __name__==’__main__’:
main()
Explanation is simple, I create an object for UdmsLDAP Class which is in the module udmsldap (see in the import section) and call two member functions get_ous and get_users and parse the ou and users using the functions parse_ous and parse_users and display the same, respectively.
- import ldap
import string
import ldap.modlistclass UdmsLDAP:
def __init__(self,domain_str,bind_dn,bind_pwd):
self.timeout=0
self.result_set=[]self.ldms_var=ldap.open(domain_str)try:
self.ldms_var.simple_bind(bind_dn,bind_pwd)
except ldap.LDAPError, error_message:
print “Couldnt connect to ldap server”
def get_ous(self):
self.result_set=[]
try:
result_id=self.ldms_var.search(“dc=udms,dc=com”,ldap.SCOPE_SUBTREE,”ou=*”,None)
while 1:
result_type, result_data = self.ldms_var.result(result_id, self.timeout)
if result_data == []:
break
else:
if result_type == ldap.RES_SEARCH_ENTRY:
self.result_set.append(result_data)
except ldap.LDAPError, error_message:
print error_message
return self.result_set
def get_users(self):
self.result_set=[]
try:
result_id=self.ldms_var.search(“dc=udms,dc=com”,ldap.SCOPE_SUBTREE,”uid=*”,None)
while 1:
result_type, result_data = self.ldms_var.result(result_id, self.timeout)
if result_data == []:
break
else:
if result_type == ldap.RES_SEARCH_ENTRY:
self.result_set.append(result_data)
except ldap.LDAPError, error_message:
print error_message
return self.result_set
def add_entry(self):
try:
self.dn=’mc=client2,dc=udms,dc=com’
self.modlist=[]
self.modlist.append((‘objectClass’,['top','udms']))
self.modlist.append((‘mc’,'client2′))
self.modlist.append((‘ip’,’192.168.1.4′))
self.ldms_var.add_s(self.dn,self.modlist)
except ldap.LDAPError,error_message:
print error_message
I start off with initializing the ldap variable in the constructor. The two things which are to be noted are,
self.ldms_var=ldap.open(domain_str)
the open function opens a connection to the ldap server.
self.ldms_var.simple_bind(bind_dn,bind_pwd)
the bind function is to bind us with the ldap server to perform the directory services functions.
Then, we have our member functions,
get_ous
Here I perform a ldap search for the object class Organization Unit.
self.ldms_var.search(“dc=ldms,dc=com”,ldap.SCOPE_SUBTREE,”ou=*”,None)
And get the results in the resultant set of arrays and return back to main.py for parsing and displaying them.
get_users
Here I perform a ldap search for the object class uid.
self.ldms_var.search(“dc=ldms,dc=com”,ldap.SCOPE_SUBTREE,”uid=*”,None)
And get the results in the resultant set of arrays and return back to main.py for parsing and displaying them.
The parsing done in main.py is self explanatory
Hope this helps to start off with PyLDAP







Thanks for the info! In the future, use
tags to separate code from other text.