使用 python-ldap 添加组

adding group using python-ldap

我需要添加多个成员的群组。所以我在下面使用这段代码

import ldap
import ldap.modlist as modlist

# Open a connection
l = ldap.initialize("ldap://localhost:389/")

# Bind/authenticate with a user with apropriate rights to add objects
l.simple_bind_s("cn=manager,dc=maxcrc,dc=com","secret")

# The dn of our new entry/object
dn="cn=semuaFK,ou=group,dc=maxcrc,dc=com" 

# A dict to help build the "body" of the object
attrs = {}
attrs['objectclass'] = ['top','groupofnames']
attrs['member'] = 'cn=user1,ou=people,dc=maxcrc,dc=com'
attrs['member'] = 'cn=user2,ou=people,dc=maxcrc,dc=com'
attrs['description'] = 'ini group untuk semua dosen dokter'

# Convert our dict to nice syntax for the add-function using modlist-module
ldif = modlist.addModlist(attrs)

# Do the actual synchronous add-operation to the ldapserver
l.add_s(dn,ldif)

# Its nice to the server to disconnect and free resources when done
l.unbind_s()

正在将成员添加到组中

attrs['member'] = 'cn=user1,ou=people,dc=maxcrc,dc=com'
attrs['member'] = 'cn=user2,ou=people,dc=maxcrc,dc=com'

但结果是一个只有一个成员的组。只有 user2 被添加到组中。如何建群,还要加几个成员

member 是一个多值属性(就像 objectClass)。通过查看您的代码,问题似乎是您用第二行覆盖了 member 属性的值。代码应该是这样的:

attrs['member'] = [ 'cn=users1,ou=people,dc=maxcrc,dc=com', 'cn=users2,ou=people,dc=maxcrc,dc=com' ]`

attrs['member'] = [ 'cn=users1,ou=people,dc=maxcrc,dc=com' ]
attrs['member'] += 'cn=users2,ou=people,dc=maxcrc,dc=com'