gpt4 book ai didi

python - ldap python result3 函数结果中缺少 LDAP 属性,但出现在 ldapsearch 命令结果中

转载 作者:行者123 更新时间:2023-12-02 11:45:00 25 4
gpt4 key购买 nike

我们有 python 应用程序,它从 ldap 读取条目并更新其他数据库中的用户。现在我遇到的问题是,当我运行时

ldapsearch 'sAMAccountName=myID` 

我能够成功获取属性employeeID

但是从 python 代码中我无法打印 employeeID 属性。

我的Python代码如下:

import ldap
import datetime
conn = ldap.initialize('ldap://url:port')
conn.simple_bind_s('CN=ABC,OU=XYZ,OU=AAA,DC=users,DC=net', 'password')
from ldap.controls import SimplePagedResultsControl

LDAP_ATTRS = ['*']
LDAP_FILTER = (
'(&(sAMAccountName=myId))'
)

pager = SimplePagedResultsControl(
criticality=True,
size=1500000,
cookie=''
)

msgid = conn.search_ext(
'DC=users,DC=net', ldap.SCOPE_SUBTREE,
LDAP_FILTER.format(''),
LDAP_ATTRS, serverctrls=[pager]
)

rtype, rdata, rmsgid, serverctrls = conn.result3(msgid)

for dn, entry in rdata:
for ent in entry:
print(ent)

我不知道为什么我无法打印其中一个属性 (employeeID)。由 ldapsearch 命令提供。

编辑

首先,我对某些假设表示怀疑,例如用户应该有权读取属性。但我不确定我的假设是否正确。

由于我是 python 和 ldap 的新手,所以我阅读了有关 ldap 模式的内容,并根据模式阅读了有关结果的结果。如果有人解释一下他们的理解,这将有助于分析和理解。

最佳答案

我更喜欢使用 ldap3 而不是 ldaphttps://ldap3.readthedocs.io/

这是因为 sAMAccountName 是您条目的一个属性。

你能试试这个代码吗?

from ldap3 import Server, Connection, SUBTREE, ALL_ATTRIBUTES
import json

server = Server("ldap://url:port")
connection = Connection(server, "CN=ABC,OU=XYZ,OU=AAA,DC=users,DC=net", "password")
connection.search(
"DC=users,DC=net",
"(objectCategory=user)",
search_scope=SUBTREE,
attributes=ALL_ATTRIBUTES,
)
data = json.loads(connection.response_to_json())

entries = data["entries"]
for entry in entries:
print(entry["attributes"])
print(entry["attributes"]["sAMAccountName"])

关于python - ldap python result3 函数结果中缺少 LDAP 属性,但出现在 ldapsearch 命令结果中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60153375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com