我正在尝试使用ldap3版本'0.9.7.4' 将一些代码更新为python3 .(https://pypi.python.org/pypi/ldap3)
以前,我使用python-ldap和python2来验证这样的用户:
import ldap
address = "ldap://HOST:389"
con = ldap.initialize(address)
base_dn = "ourDN=jjj"
con.protocol_version = ldap.VERSION3
search_filter = "(uid=USERNAME)"
result = con.search_s(base_dn, ldap.SCOPE_SUBTREE, search_filter, None)
user_dn = result[0][0] # get the user DN
con.simple_bind_s(user_dn, "PASSWORD")
Run Code Online (Sandbox Code Playgroud)
这将正确返回(97, [], 2, [])正确的密码,并ldap.INVALID_CREDENTIALS使用不正确的密码引发绑定尝试.
ldap3在python3中使用我正在做以下事情:
from ldap3 import Server, Connection, AUTH_SIMPLE, STRATEGY_SYNC, ALL
s = Server(HOST, port=389, get_info=ALL)
c = Connection(s, authentication=AUTH_SIMPLE, user=user_dn, password=PASSWORD, check_names=True, lazy=False, client_strategy=STRATEGY_SYNC, raise_exceptions=True)
c.open()
c.bind()
Run Code Online (Sandbox Code Playgroud)
它引发了以下异常:
ldap3.core.exceptions.LDAPInvalidCredentialsResult: LDAPInvalidCredentialsResult - …Run Code Online (Sandbox Code Playgroud) 我正在处理我认为是一个简单的问题,但它真的很困惑。我正在使用ldap3模块在 Python 中进行 AD 查询。此查询将数据作为 python 词典的生成器返回。当我type()对数据运行命令时,它确认它是一个字典。
我需要将其转换为 JSON,以便将其写入日志文件。首先,我尝试过,json.dump(data, file)但是当我这样做时,我收到此错误: TypeError: Object of type 'CaseInsensitiveDict' is not JSON serializable
如果我首先尝试将字典转换为字符串,则数据用引号写入,内部字段用单引号留下,因此它不是真正的 JSON - 它看起来像这样: "{'key': 'value'}"
这是我所拥有的:
with open(outfile, 'w') as outfile:
for entry in entry_generator: # Entry is equal to a dictionary of AD attributes
json.dump(entry, outfile) # I have also tried json.dump(str(entry), outfile) etc
Run Code Online (Sandbox Code Playgroud)
我一直在搜索互联网,我看到很多关于requests模块的这个问题,但似乎没有任何东西可以解决我的情况。
对于 ldap3 模块entry_to_json(),我在其他脚本中使用了一种方法。由于生成器,它似乎在这里不起作用,如果有人知道如何再次使其工作,它可以解决我的问题。想法?
嗨,我正在尝试从ldap3库中导入“ LDAPBindError”,但出现导入错误。有人可以解释为什么这不起作用吗?
from ldap3 import Server, Connection, ALL, LDAPBindError, SUBTREE, ALL_ATTRIBUTES,MODIFY_REPLACE
ImportError: cannot import name 'LDAPBindError'
Run Code Online (Sandbox Code Playgroud)
我正在使用以下版本
pip3.6列表| grep ldap3 ldap3(2.5)
python3.6-版本Python 3.6.4
我正在使用 Python 中的 ldap3 模块连接到本地 AD 域(在我的 vbox 机器上,Server 2016 域控制器上),如何阻止搜索结果返回引用?我已经设置了我可以在其他帖子和文档中找到的内容,但推荐仍然存在。
代码:
#!/usr/bin/env python
from ldap3 import Server, Connection, AUTO_BIND_NO_TLS, SUBTREE, ALL_ATTRIBUTES, ALL, DEREF_NEVER
from pprint import pprint
def get_ldap_info():
with Connection(Server('dc01.ad.local', port=389, use_ssl=False),
auto_bind=AUTO_BIND_NO_TLS,
auto_referrals=False,
read_only=True,
check_names=True,
user='CN=Administrator,CN=Users,dc=ad,dc=local',
password='XXX') as c:
results = c.extend.standard.paged_search(search_base='dc=ad,dc=local',
search_filter='(objectClass=computer)',
search_scope=SUBTREE,
attributes=ALL_ATTRIBUTES,
dereference_aliases=DEREF_NEVER,
get_operational_attributes=False)
i = 0
for item in results:
print "---------"
print type(item)
print("TYPE ATTR: %s" % (item['type']))
pprint(item)
i += 1
print(i)
if __name__ == "__main__":
get_ldap_info()
Run Code Online (Sandbox Code Playgroud)
运行产生以下作为前 3 个结果:
--------- …Run Code Online (Sandbox Code Playgroud) 我正在开发 django 网站,我想在我的应用程序中使用 ldap 身份验证。我正在使用 Django 1.11 通过 django-python3-ldap 进行身份验证。我使用 ldapsearch 测试了与 ldap 的连接,结果成功,得到以下结果:
ally@websrv:/web/demo_project_ally$ ldapsearch -x -W -D 'cn=admin,dc=myldap,dc=com' -b "dc=myldap,dc=com" "cn=ally ally" -H ldap://myldap.com
Enter LDAP Password:
# extended LDIF
#
# LDAPv3
# base <dc=myldap,dc=com> with scope subtree
# filter: cn=ally ally
# requesting: ALL
#
# ally ally, my_ou, myldap.com
dn: cn=ally ally,ou=my_ou,dc=myldap,dc=com
cn: ally ally
givenName: ally
gidNumber: 500
homeDirectory: /home/users/aally
sn: ally
loginShell: /bin/sh
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: top
userPassword:: e01ENX1kNVJuSkw0bTV3RzR3PT0=
uidNumber: 1000 …Run Code Online (Sandbox Code Playgroud) 我正在获取 Active Directory 中所有用户的列表,并且我需要检查其状态 \xe2\x80\x94 如果用户处于活动状态或已禁用状态。我希望userAccountControl应该返回用户状态,但除了一个(返回 66048)之外的所有用户,我只得到 512,并且这与用户状态无关(据我所知)。
\n\nfrom ldap3 import Server, Connection\n\nserverName = 'LDAP://server'\ndomainName = 'name'\nuserName = 'superuser'\npassword = 'password'\nbase = 'longString'\n\nserver = Server(serverName)\nconn = Connection(server, read_only=True, user='{0}\\\\{1}'.format(domainName, userName), password=password, auto_bind=True)\n\nconn.search(base, '(objectclass=person)', attributes=['displayName', 'mail', 'userAccountControl','sAMAccountName'])\n\nfor i in conn.entries:\n print 'USER = {0} : {1} : {2}'.format(i.sAMAccountName.values[0], i.displayName.values[0], i.userAccountControl.values[0])\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n\n用户 = ABC:约翰·史密斯:512\n 用户 = DEF:莎拉·康纳:514\n 用户 = GHI:托马斯·安德森:66048
\n
这是获取用户状态的正确方法吗?还有其他方法可以通过某些带有 UI 的应用程序检查 AD 用户状态吗?
\nIf I already have an LDAP DN, how do I get the attributes for that DN with ldap3.Connection.search()? There is no other search criteria, I already have the DN...
I tried searching for dn attribute, but it returned no objects found. I also tried forcing search_filter to '', '()' or None and they all returned malformed filter string.
I also couldn't find a way to do this with the abstract Reader...
In ldapsearch you don't need to specify …
我需要使用 LDAP 从服务器获取数据。我正在使用 ldap3,并且正在获取我需要的所有属性和响应,但是它们被困在 ldap.abstract.entry.entry 类中。我尝试了几种方法将数据转换为 json 或列表形式,但它从未按照我预期的方式工作。
例如我尝试过:
entry = conn.entries
current_data = (entry.entry_to_json())
print(type(current_data))
print(len(current_data))
>>> <class 'str'>
>>> 480
Run Code Online (Sandbox Code Playgroud)
然而,这导致我的所有数据都是长度为 480 或更长的单个字符串。我尝试过使用这个,但事实证明这非常困难。所以我尝试以下方法将字符串转换为字典:
current_data = (entry[0].entry_to_json())
current_data = ast.literal_eval(current_data)
print(type(current_data))
print(len(current_data))
>>> <class 'dict'>
>>> 2
Run Code Online (Sandbox Code Playgroud)
但是,现在我无法单独访问所有不同的属性。我的目标是将从 LDAP 查询获得的所有数据写入 CSV,以便其他应用程序可以使用它。但目前我陷入困境,因为我无法将数据转换为 JSON 或 CSV 格式。
如何获取并处理来自 的数据,conn.entries以便将每个属性写入 CSV 中的单独列?
最好的祝愿,
C·泽布
编辑:
这是我使用时得到的示例(entry.entry_to_json()):
{
"attributes": {
"dcxLDOUniqueCN": [
"XXXX"
],
"dcxPostalAddress": [
"XXX"
],
"dcxSupervisor": [
"XXX"
],
"mail": [
"XXX"
],
"mobile": [],
"uid": …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写 python 代码,它将使用 LDAP 模块来验证 LDAP 连接:
import configuration
from ldap3 import Server, Connection, SIMPLE, SYNC, ALL
server = Server(configuration.LDAP_SERVER, port=XXXX, get_info=ALL)
c = Connection(server, authentication=SIMPLE, user=configuration.LDAP_USER, password=configuration.LDAP_PASS, check_names=True, lazy=False, client_strategy=SYNC, raise_exceptions=False)
c.open()
c.bind()
Run Code Online (Sandbox Code Playgroud)
运行代码时,我得到:
{'result': 49, 'description': 'invalidCredentials', 'dn': '', 'message': '80090308: LdapErr: DSID-0C09042A, comment: AcceptSecurityContext error, data 52e, v3839\x00', 'referrals': None, 'saslCreds': None, 'type': 'bindResponse'}
Run Code Online (Sandbox Code Playgroud)
我确信我使用的用户名和密码是正确的。你能告诉我代码有什么问题吗?
使用以下代码可以使用 ldap3 模块在 Python 3.8.x 中设置连接对象。
tls = Tls(validate=ssl.CERT_NONE, version=ssl.PROTOCOL_TLSv1_2)
server = Server(server_uri, use_ssl=True, tls=tls, get_info=ALL)
conn = Connection(server, user="domain\\myusername", password="password", authentication=NTLM, auto_referrals=False)
conn.bind()
Run Code Online (Sandbox Code Playgroud)
但从 Python 3.10 开始,此代码不再有效 - 执行时会导致 SSL 握手错误。我相信这是因为当前 Python 3.10.x 包中使用的 OpenSSL 库发生了变化。
还有其他人遇到过这个问题并希望找到解决方案吗?
ldap3 ×10
python ×8
python-3.x ×4
dictionary ×2
ldap ×2
credentials ×1
django ×1
json ×1
openldap ×1
python-3.6 ×1
ssl ×1
tls1.2 ×1