Python 图形界面 AD 域账号密码同步
要实现两个不同的 AD 域之间的账号密码同步,可以使用 Python 的 AD 模块来连接和操作 AD 域。
首先,需要安装 Python 的 AD 模块,可以使用命令pip install python-ldap来进行安装。
下面是一个示例代码,演示了如何连接两个 AD 域,将一个 AD 域的账号密码同步到另一个 AD 域:
import ldap
# 连接第一个 AD 域
source_ldap = ldap.initialize('ldap://source_domain_controller')
source_ldap.simple_bind_s('source_username', 'source_password')
# 连接第二个 AD 域
target_ldap = ldap.initialize('ldap://target_domain_controller')
target_ldap.simple_bind_s('target_username', 'target_password')
# 搜索第一个 AD 域的用户
results = source_ldap.search_s('dc=source_domain,dc=com', ldap.SCOPE_SUBTREE, '(objectClass=user)')
# 遍历搜索结果,将账号密码同步到第二个 AD 域
for dn, attrs in results:
if 'userPrincipalName' in attrs:
username = attrs['userPrincipalName'][0].decode('utf-8')
password = attrs['unicodePwd'][0].decode('utf-8')
# 添加或更新用户到第二个 AD 域
target_ldap.modify_s(dn, [(ldap.MOD_REPLACE, 'userPassword', password.encode('utf-16-le'))])
# 断开连接
source_ldap.unbind()
target_ldap.unbind()
请注意,上述代码仅演示了如何进行账号密码同步,实际应用中还需要根据实际需求进行适当的修改和错误处理。同时,还需要确保在运行代码时具有足够的权限来连接和操作 AD 域。
原文地址: https://www.cveoy.top/t/topic/coMq 著作权归作者所有。请勿转载和采集!