首页 > 解决方案 > Python ldap3 创建组

问题描述

我是 python 和 ldap3 模块的新手。但是我想在特定的 OU 中创建一个 AD 组。如何才能做到这一点?

# import class and constants
from ldap3 import Server, Connection, ALL

# define the server
s = Server('servername', get_info=ALL)  # define an unsecure LDAP server, 

# define the connection
c = Connection(s, user='user_dn', password='user_password')

ou = "OU=Staff,OU=RU,DC=DOMAIN,DC=LOCAL"
groupname="ADM_Local"
description="local group for access to IPA"

如何ADM_Local在定义的 ou 中添加组并将描述添加到组中?该文档没有说明它是如何完成的:https ://ldap3.readthedocs.io/tutorial_operations.html#create-an-entry

标签: pythonpython-3.xldapldap3

解决方案


您需要使用groupOfNames结构的 objectClass(或派生的)。请注意,根据您的 ldap 服务器实现,member可能需要该属性来防止创建空组。

groupDN = 'cn=ADM_Local,ou=Staff,ou=RU,dc=domain,dc=local'
objectClass = 'groupOfNames'
attr = {
  'cn': 'ADM_Local',
  'member': 'uid=admin,ou=people,dc=domain,dc=local',
  'description': 'local group for access to IPA'
}

c.add(groupDN , objectClass , attr)

推荐阅读