How to Read LDAP Data with Python
LDAP (Lightweight Directory Access Protocol) is a protocol used for accessing and managing directory services data, such as user information. Python provides several libraries for interacting with LDAP servers, and one popular choice is the ldap3
library. In this blog post, we’ll explore how to use Python to read LDAP data and save it to an Excel file using the ldap3
library.
Prerequisites
Before we begin, make sure you have the following prerequisites in place:
- Python installed on your system.
- The
ldap3
library installed. You can install it using pip:
pip install ldap3
- The
openpyxl
library installed for working with Excel files:
pip install openpyxl
Reading LDAP Data with Python
We’ll demonstrate how to read LDAP data and save it to an Excel file using Python. Below is a Python script that accomplishes this task:
from ldap3 import Server, Connection
from openpyxl import Workbook
def Ldap(OuUser, LdapUser, Filename):
# Create a connection to the LDAP server
server = Server('mydomain.com')
conn = Connection(server, authentication="SIMPLE", user="user@mail.com", password="Password")
conn.bind()
# Search for LDAP entries based on the provided filter
result = conn.search(search_base=OuUser, search_filter='(objectClass=user)', attributes=LdapUser)
# Create a new Excel workbook and worksheet
wb = Workbook()
ws = wb.active
ws.append(LdapUser)
# Iterate through LDAP entries and retrieve desired attributes
for entry in conn.entries:
attributes_values = []
for attribute in LdapUser:
if attribute in entry:
attributes_values.append(entry[attribute].value)
else:
attributes_values.append(None)
ws.append(attributes_values)
# Save the Excel workbook
wb.save(Filename)
# Unbind the LDAP connection
conn.unbind()
# Define the LDAP user attributes and search base
LdapUser = ['sAMAccountName', 'title', 'givenName', 'sn', 'company', 'department', 'streetAddress', 'postalCode', 'l', 'co', 'telephoneNumber', 'mobile', 'mail', 'extensionAttribute11', 'manager']
OuUser = 'ou=users,ou=accounts,ou=test,dc=mydomain,dc=com'
# Call the Ldap function to retrieve user data and save it to an Excel file
Ldap(OuUser, LdapUser, "c:\\python\\ldap\\ldap_user.xlsx")
# Define the LDAP computer attributes and search base
LdapComputer = ['sAMAccountName', 'title', 'cn', 'description', 'managedBy', 'operatingSystem', 'operatingSystemVersion']
OuComputer = 'ou=mobile,ou=computers,ou=test,dc=mydomain,dc=com'
# Call the Ldap function to retrieve computer data and save it to an Excel file
Ldap(OuComputer, LdapComputer, "c:\\python\\ldap\\ldap_mobile.xlsx")
Understanding the Code
- Import necessary libraries: We import the
Server
,Connection
class fromldap3
and theWorkbook
class fromopenpyxl
. - Create an LDAP connection: We create a connection to the LDAP server with the provided credentials.
- Search for LDAP entries: We use the
search
method to query LDAP entries based on the provided search filter and attributes. - Create an Excel workbook and worksheet: We initialize an Excel workbook and add a worksheet to it.
- Iterate through LDAP entries: We loop through the LDAP entries and retrieve the desired attributes. If an attribute is missing in an entry, we append
None
to the Excel sheet. - Save the Excel workbook: We save the workbook to the specified file location.
- Unbind the LDAP connection: We disconnect from the LDAP server when we are done.
Conclusion
In this blog post, we’ve demonstrated how to read LDAP data with Python using the ldap3
library and save it to an Excel file. This can be a useful technique for managing and analyzing directory services data efficiently.