python ldap3 创建用户_ldap3 python将用户添加到组

本文介绍了一个使用python-ldap3库创建假用户和组的脚本,并解决将用户链接到组的问题。通过调用ldap3.extend.microsoft.addMembersToGroups模块的ad_add_members_to_groups方法,成功将用户添加到AD组。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I'm writing a small script using python-ldap3 to generate dummy users and groups.

I'm having trouble linking a user with a group. After running this snippet there are no changes in my Active Directory server:

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

What's wrong?

解决方案

Use ldap3.extend.microsoft.addMembersToGroups

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

...

addUsersInGroups(conn, user_dn, group_dn)

Full script:

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值