共計 938 個字符,預計需要花費 3 分鐘才能閱讀完成。
要獲取 LDAP(輕型目錄訪問協議)中的所有用戶,可以使用 python-ldap 庫。
首先,確保安裝了 python-ldap 庫??梢允褂靡韵旅钸M行安裝:
pip install python-ldap
下面是一個示例代碼,演示如何連接到 LDAP 服務器 并獲取所有用戶信息:
import ldap
# 連接到 LDAP服務器
ldap_server = 'ldap://your_ldap_server' # 替換為實際的 LDAP 服務器地址
ldap_base_dn = 'dc=example,dc=com' # 替換為實際的 LDAP 基礎 DN
ldap_user = 'cn=admin,dc=example,dc=com' # 替換為實際的 LDAP 管理員用戶
ldap_password = 'password' # 替換為實際的 LDAP 管理員用戶密碼
conn = ldap.initialize(ldap_server)
conn.simple_bind_s(ldap_user, ldap_password)
# 搜索所有用戶
search_filter = '(objectClass=person)'
search_attribute = ['cn', 'mail'] # 指定要獲取的屬性列表
results = conn.search_s(ldap_base_dn, ldap.SCOPE_SUBTREE, search_filter, search_attribute)
# 打印用戶信息
for dn, attrs in results:
print('User DN:', dn)
for attr, values in attrs.items():
print(attr + ':', values)
# 斷開 LDAP 連接
conn.unbind()
在上面的示例代碼中,首先使用 ldap.initialize()
方法初始化一個 LDAP 連接,然后使用 conn.simple_bind_s()
方法進行身份驗證。接下來,使用 conn.search_s()
方法搜索所有用戶。搜索時,指定了要搜索的基礎 DN、搜索范圍、搜索過濾器和要獲取的屬性列表。最后,使用循環遍歷結果,并打印每個用戶的屬性信息。
請根據實際情況替換示例代碼中的 LDAP 服務器地址、基礎 DN、管理員用戶和密碼。
丸趣 TV 網 – 提供最優質的資源集合!
正文完