溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python使用ldap3操作微軟AD

發(fā)布時間:2020-08-01 01:16:10 來源:網(wǎng)絡 閱讀:41441 作者:殘風琴德 欄目:編程語言

對于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(一個數(shù)值),異步策略發(fā)送請求后不用等待響應,需要響應的時候直接使用get_response(message_id)獲取結果。等待響應的超時時間可以通過get_responsetimeout參數(shù)指定,默認10s。如果使用get_request=True in the get_response(),同時會返回發(fā)送的請求字典。

 

建立連接:

Python使用ldap3操作微軟AD

Python使用ldap3操作微軟AD


Python使用ldap3操作微軟AD

Python使用ldap3操作微軟AD


建立Server對象時使用get_info=ldap3.ALL參數(shù),建立Connection連接之后可以獲取到server信息(匿名獲取),從中可以獲取到域名信息,域控計算機名,ldap server支持的ExtensionControl

建立Server時指定 active=True,建立連接前會先檢查ldap server的可用性;active=5指定拋出 LDAPServerPoolExhaustedError異常之前重試的次數(shù)

 exhaust=True : 如果ldap server不時active,server將會從pool中移除。exhaust=10:設置為數(shù)值,表示認為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 ‘a(chǎn)ctive’ 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 serverSchema數(shù)據(jù)庫中存儲了ldap server中的對象的已知類型信息,可以通過server.schema獲取到(微軟AD需要鑒權,匿名用戶無法獲?。锩娲鎯α?/span>ldap server理解那些數(shù)據(jù)類型,同時也指定,哪些屬性被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_basesearch_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,只返回entriesdistinguished Name,請求成功(同步strategy)返回True,conn.entries獲取查詢到的結果:

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

conn.entries

Python使用ldap3操作微軟AD

可以使用訪問字典或者訪問對象屬性的方式訪問從server上獲取到的attribute值,有些屬性不區(qū)分大小寫,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,第一個參數(shù)為增加的對象dn,第二個參數(shù)為object_class,指定創(chuàng)建的object的類型,第三個參數(shù)為object提供的個性化attribute:


 Python使用ldap3操作微軟AD

域控支持的objectclass可以通過server.schema獲取到,創(chuàng)建不同類型的objectclass支持哪些attribute可以通過server.schema.object_classes['user']方式獲取到,大多數(shù)attribute在創(chuàng)建object的時候都是可選的,必選參數(shù)會單獨列出:

Python使用ldap3操作微軟AD

Python使用ldap3操作微軟AD


重命名一個dn,利用modify_dn提供的參數(shù)new_superior=new_dn,還可以將dn從一個ou移動到另一個ou:

Python使用ldap3操作微軟AD

Python使用ldap3操作微軟AD

 

檢查object的屬性是否和給定值一樣。

Python使用ldap3操作微軟AD


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI