ldap3 python search members of a group and retrieve their sAMAcountName (Active Directory)(ldap3 python 搜索组成员并检索他们的 sAMAcountName (Active Directory))
问题描述
我正在尝试将 ldap3 与 python 一起使用来检索组的成员并检索他们的 sAMAccountName,因为我们混合了 DN(一些带有 NTID,其他带有名字/姓氏).
I'm trying to use ldap3 with python to retrieve members of a group and also retrieve their sAMAccountName as we have mixed DN's (some with NTID and others with first/last name).
我一直在尝试这个没有 0 运气,任何帮助将不胜感激:
I've been trying this with no 0 luck, any help would be appreciated:
from ldap3 import Server, Connection, AUTO_BIND_NO_TLS, SUBTREE, BASE,
ALL_ATTRIBUTES, ObjectDef, AttrDef, Reader, Entry, Attribute,
OperationalAttribute import ldap3
conn = Connection(Server('adserver.com', port=389, use_ssl=False),
auto_bind=AUTO_BIND_NO_TLS, user='DOMAIN\NTID',
password='somepassword')
conn.search(search_base='CN=GROUPNAME,OU=Groups,OU=Resources,OU=Global,DC=adserver.com',
search_filter='(objectCategory=person)', search_scope=SUBTREE,
attributes = ['sAMAAccountName'], size_limit=0)
print(conn.response_to_json())
推荐答案
在搜索成员之前,您必须先从组本身中下拉成员列表.
Before you can search the members you must first pull down the list of members from the group itself.
conn.search(
search_base='CN=GROUPNAME,OU=Groups,OU=Resources,OU=Global,DC=adserver.com',
search_filter='(objectClass=group)',
search_scope='SUBTREE',
attributes = ['member']
)
for entry in conn.entries:
print(entry.member.values)
这将打印出成员列表作为专有名称.
This will print out a list of members as distinguished names.
然后您需要执行一个新的搜索,遍历每个成员并返回每个成员的 sAMAccountName.
You will then need to perform a new search that iterates through each of the members and returns the sAMAccountName for each member.
下面是完整代码的样子(可能需要调整):
Here is what the full code might look like (may need to be tweaked):
conn.search(
search_base='CN=GROUPNAME,OU=Groups,OU=Resources,OU=Global,DC=adserver.com',
search_filter='(objectClass=group)',
search_scope='SUBTREE',
attributes = ['member']
)
for entry in conn.entries:
for member in entry.member.values:
conn.search(
search_base='OU=Global,DC=adserver.com',
search_filter=f'(distinguishedName={member})',
attributes=[
'sAMAccountName'
]
)
user_sAMAccountName = conn.entries[0].sAMAccountName.values
print(user_sAMAccountName)
这篇关于ldap3 python 搜索组成员并检索他们的 sAMAcountName (Active Directory)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ldap3 python 搜索组成员并检索他们的 sAMAcountName (Active Directory)
基础教程推荐
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 筛选NumPy数组 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01