本文介绍了ldap3 python将用户添加到组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python-ldap3编写一个小的脚本,以生成虚拟用户和组.

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

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

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'])]})

怎么了?

推荐答案

使用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)

这篇关于ldap3 python将用户添加到组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-05 10:33