gpt4 book ai didi

python-3.x - ldap3 python 将用户添加到组

转载 作者:行者123 更新时间:2023-12-01 17:16:25 25 4
gpt4 key购买 nike

我正在使用 python-ldap3 编写一个小脚本来生成虚拟用户和组。

我在将用户与组链接时遇到问题。运行此代码片段后,我的 Active Directory 服务器中没有任何更改:

conn.modify('cn=dancing,ou=test-groups,dc=stand,dc=lsd', {'memberuid': [(MODIFY_REPLACE, ['cn=User1, ou=users,dc=stand,dc=lsd'])]})

出了什么问题?

最佳答案

使用ldap3.extend.microsoft.addMembersToGroups

from ldap3.extend.microsoft.addMembersToGroups import ad_add_members_to_groups as addUsersInGroups

...

addUsersInGroups(conn, user_dn, group_dn)
<小时/>

完整脚本:

from ldap3 import Server, Connection, ALL, NTLM
from elizabeth import Personal, Address,Text
from ldap3.extend.microsoft.addMembersToGroups import ad_add_members_to_groups as addUsersInGroups
import random

serverName='dc1.stand.local'
connUser="stand.lsd\\Admin"
connUserPwd=""
usersOU = 'ou=test-ou,dc=stand,dc=local'
groupsOU = 'ou=test-groups,dc=stand,dc=local'

usersDnList = []
groupsDnList = []

server = Server(serverName, get_info=ALL)
conn = Connection(server, user=connUser, password=connUserPwd, authentication=NTLM)
conn.bind() #must be TRUE

conn.add(usersOU, 'organizationalUnit') # add test-ou for users
conn.add(groupsOU, 'organizationalUnit') # add test-ou for groups

data = Text('en')
for _ in range(0,10):
currentGroup = 'cn='+data.word()+',ou=test-groups,dc=stand,dc=local'
groupsDnList.append(currentGroup)
conn.add(currentGroup, 'group')

address = Address('en')
person = Personal('en')
for _ in range(0,10):
address_country = address.country()
conn.add('ou='+address_country+',ou=test-ou,dc=stand,dc=local', 'organizationalUnit')
for _ in range (0,10):
name = person.name(gender='male')
surname = person.surname(gender='male')
currentUser = 'cn='+name+'.'+surname+','+'ou='+address_country+',ou=test-ou,dc=stand,dc=local'
usersDnList.append(currentUser)
conn.add(currentUser, 'User',
{'givenName': name,
'sn': surname,
'departmentNumber': 'DEV',
'telephoneNumber': 1111})

for _ in range(0,300):
rndUser = random.choice(usersDnList)
rndGroup = random.choice(groupsDnList)
addUsersInGroups(conn, rndUser, rndGroup)

关于python-3.x - ldap3 python 将用户添加到组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44609358/

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