前言
公司部门培训用到 ldap3,布置了个作业,于是开始看官方文档学习中。我是直接从 LDAP Operations 部分开始看的。
主要就是官方文档提供了增删改查的接口,需要看懂函数和参数,然后就会用了。
增加操作
官方 add 函数
def add(self,
dn,
object_class=None,
attributes=None,
controls=None)
逐个参数解释:
-
dn:标识要添加的目标名字
-
object_class:要添加的标志类名称,可以是包含一个单一值或一串字符串
-
attributes:一个以 {‘attr1’: ‘val1’, ‘attr2’: ‘val2’, …} or {‘attr1’: [‘val1’, ‘val2’, …], …} 多值形式的字典
-
controls:发送请求额外的信息
举例
# import class and constants
from ldap3 import Server, Connection, ALL
# define the server
s = Server('servername', get_info=ALL) # define an unsecure LDAP server, requesting info on DSE and schema
# define the connection
c = Connection(s, user='user_dn', password='user_password')
# perform the Add operation
c.add('cn=user1,ou=users,o=company', ['inetOrgPerson', 'posixGroup', 'top'], {
'sn': 'user_sn', 'gidNumber': 0})
# equivalent to 等同上面
c.add('cn=user1,ou=users,o=company', attributes={
'objectClass': ['inetOrgPerson', 'posixGroup', 'top'], 'sn': 'user_sn', gidNumber: 0})
print(c.result)
# close the connection
c.unbind()
主要就是 add 函数传三个参数:dn、object_class、attributes。
- dn 包含用户cn、ou、o等信息
删除操作
官方 delete 函数
def delete(self,
dn,
controls=None):
逐个参数解释:
- dn:标识要删除的目标名字
- controls:发送请求额外的信息
举例
from ldap3 import Server, Connection, ALL
# define the server
s = Server('servername', get_info=ALL) # define an unsecure LDAP server, requesting info on DSE and schema
# define the connection
c = Connection(s, user='user_dn', password='user_password')
# perform the Delete operation
c.delete('cn=user1,ou=users,o=company')
print(c.result)
# close the connection
c.unbind()
主要就是 delete 函数传一个参数:dn。
- dn 包含用户cn、ou、o等信息
修改操作
官方 modify 函数
def modify(self,
dn,
changes,
controls=None):
逐个参数解释:
- dn:标识要删除的目标名字
- changes:一个要被展示在具体入口的修改的字典
- controls:发送请求额外的信息
举例
# import class and constants