Python使用ldap3操作微软AD
admin
2023-07-24 19:42:52
0次

对于client连接ldap server的策略,ldap3提供了4种选择,可以通过client_strategy设置Connection object应用哪种策略:

l  SYNC

l  ASYNC

l  RESTARTABLE

l  REUSABLE

同步策略(SYNC, RESTARTABLE),所有的ldap操作返回True/False

异步策略(ASYNC, REUSABLE)返回一个msgid(一个数值),异步策略发送请求后不用等待响应,需要响应的时候直接使用get_response(message_id)获取结果。等待响应的超时时间可以通过get_response的timeout参数指定,默认10s。如果使用get_request=True in the get_response(),同时会返回发送的请求字典。

 

建立连接:

Python使用ldap3操作微软AD

Python使用ldap3操作微软AD


Python使用ldap3操作微软AD

Python使用ldap3操作微软AD


建立Server对象时使用get_info=ldap3.ALL参数,建立Connection连接之后可以获取到server信息(匿名获取),从中可以获取到域名信息,域控计算机名,ldap server支持的Extension和Control

建立Server时指定 active=True,建立连接前会先检查ldap server的可用性;active=5指定抛出 LDAPServerPoolExhaustedError异常之前重试的次数

 exhaust=True : 如果ldap server不时active,server将会从pool中移除。exhaust=10:设置为数值,表示认为server 10s不可达,则认为它为offline,

Python使用ldap3操作微软AD

When all servers in a pool are not available the strategy will wait for the number of seconds specified in ldap.

POOLING_LOOP_TIMEOUT before starting a new cycle. This defaults to 10 seconds.

The pool can have different HA strategies:

• FIRST: gets the first server in the pool, if ‘active’ is set to True gets the first available server

• ROUND_ROBIN: each time the connection is open the subsequent server in the pool is used. If active is set to

True unavailable servers will be discarded

• RANDOM: each time the connection is open a random server is chosen in the pool. If active is set to True

unavailable servers will be discarded

A server pool can be defined in different ways:

server1 = Server('server1')

server2 = Server('server2')

server3 = Server('server1', port=636, use_ssl=True)

• explicitly with Server objects in the init:

server_pool = ServerPool([server1, server2, server3], POOLING_STRATEGY_ROUND_

˓→ ROBIN, active=True, exhaust=True)

• explicitly with an add operation in the pool object:

server_pool = ServerPool(None, POOLING_STRATEGY_ROUND_ROBIN_ACTIVE)

server_pool.add(server1)

server_pool.add(server2)

server_pool.add(server3)

44 Chapter 1. Contents

ldap3 Documentation, Release 2.5

• implicitly directly in the Connection object init (passing a list of servers):

conn = Connection([server1, server2, server3]) # the ServerPool object is

˓→ defined with the default pooling strategy

Pools can be dynamically changed. You can add and remove Server objects from pools even if they are already used

in Connection:

server4 = Server('server2', port=636, use_ssl=True)

server_pool.remove(server2)

server_pool.add(server4)

Connections are notified of the change and can reopen the socket to the new server at next open() operation.

You can also save the schema and info in a json string:

json_info = server.info.to_json()

json_schema = server.schema.to_json()

or can have them saved on file:

server.info.to_file('server-info.json')

server.schema.to_file('server-schema.json')

to build a new server object with the saved json files you can retrieve them with:

from ldap3 import DsaInfo, SchemaInfo

dsa_info = DsaInfo.from_file('server-info.json')

schema_info = SchemaInfo.from_file('server-schema.json')

server = Server('hostname', dsa_info, schema_info)

 

ldap server的Schema数据库中存储了ldap server中的对象的已知类型信息,可以通过server.schema获取到(微软AD需要鉴权,匿名用户无法获取),里面存储了ldap server理解那些数据类型,同时也指定,哪些属性被ldap server中的对象支持

Python使用ldap3操作微软AD

使用鉴权用户连接ldap server后可以查看server.shema等高级别操作。查看当前鉴权用户信息。以下连接使用的不安全的连接,密码信息明文传输,可以被抓取。使用authentication=ldap3.NTLM的鉴权方式无法显示的看到鉴权信息。


Python使用ldap3操作微软AD

Python使用ldap3操作微软AD

Python使用ldap3操作微软AD

可以使用以下方式建立安全连接,2种方式都是建立TLS连接:

l  LDAP over TLS

l  the StartTLS extended operation     ##微软AD不支持

 

ldap查询

ldap查询基于search_base和search_filter,filter是个表达式:

l  查询所有显示名叫John并且email以‘@example.org’结尾的用户:(&(givenName=John)(mail=*@example.org))

l  查询显示名为Jhon或者Fred并且邮箱以@example.org结尾的用户

(&

(|

(GivenName=Jhon)

(givenName=Fred)

)

( mail=*@example.org)

)

搜索search_base下的所有用户,默认search_scope='SUBTREE',没有指定请求任何attribute,只返回entries的distinguished Name,请求成功(同步strategy)返回True,conn.entries获取查询到的结果:

conn.search(base_search,'(objectclass=person)')

conn.entries

Python使用ldap3操作微软AD

可以使用访问字典或者访问对象属性的方式访问从server上获取到的attribute值,有些属性不区分大小写,raw_values获取到的是从server返回的原始的值:



 

Python使用ldap3操作微软AD

Python使用ldap3操作微软AD

Python使用ldap3操作微软AD

 

返回的entry可以格式化为json字符串

Python使用ldap3操作微软AD

如果查询的属性的值为空,返回的entries中将不包含此属性,除非在Connection中指定return_empty_attributes=False,微软AD中貌似不起作用。

Python使用ldap3操作微软AD

对ldap server进行search操作之后,Connection有以下属性可以访问:

Python使用ldap3操作微软AD


在AD上增加entry,第一个参数为增加的对象dn,第二个参数为object_class,指定创建的object的类型,第三个参数为object提供的个性化attribute:


 Python使用ldap3操作微软AD

域控支持的objectclass可以通过server.schema获取到,创建不同类型的objectclass支持哪些attribute可以通过server.schema.object_classes['user']方式获取到,大多数attribute在创建object的时候都是可选的,必选参数会单独列出:

Python使用ldap3操作微软AD

Python使用ldap3操作微软AD


重命名一个dn,利用modify_dn提供的参数new_superior=new_dn,还可以将dn从一个ou移动到另一个ou:

Python使用ldap3操作微软AD

Python使用ldap3操作微软AD

 

检查object的属性是否和给定值一样。

Python使用ldap3操作微软AD


相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...