2016-06-15 52 views
1

我正在使用ldap3。我想檢索AD的所有組織單位。 這裏是我的代碼如何檢索LDAP中Active Directory的所有OU?

from ldap3 import Server, Connection, SUBTREE, ALL 
total_entries = 0 

s = Server('172.30.1.197', port=636, use_ssl=True, get_info=ALL) 
admin_username = "[email protected]" 
admin_password = "[email protected]" 
c = Connection(s, user=admin_username, password=admin_password) 
c.bind() 
c.start_tls() 

c.search(search_base = 'dc=naanal,dc=local', 
     search_filter = '(objectClass=OrganizationalUnit)', 
     search_scope = SUBTREE, 
     paged_size = 5) 

total_entries += len(c.response) 

for entry in c.response: 
    print(entry) 

print('Total entries retrieved:', total_entries) 

輸出:

{'dn': u'OU=Domain Controllers,DC=naanal,DC=local', 'attributes': {}, 'raw_attributes': {}, 'type': 'searchResEntry'} 
{'dn': u'OU=Police,DC=naanal,DC=local', 'attributes': {}, 'raw_attributes': {}, 'type': 'searchResEntry'} 
{'dn': u'OU=dummy,DC=naanal,DC=local', 'attributes': {}, 'raw_attributes': {}, 'type': 'searchResEntry'} 
{'type': 'searchResRef', 'uri': ['ldaps://ForestDnsZones.naanal.local/DC=ForestDnsZones,DC=naanal,DC=local']} 
{'type': 'searchResRef', 'uri': ['ldaps://DomainDnsZones.naanal.local/DC=DomainDnsZones,DC=naanal,DC=local']} 
{'type': 'searchResRef', 'uri': ['ldaps://naanal.local/CN=Configuration,DC=naanal,DC=local']} 
('Total entries retrieved:', 6) 

中有什麼結果的最後三個條目?爲什麼要來?

回答

0

最後三個條目看起來像推介。根據文檔,轉介追逐可以被禁用。在這裏看到:http://ldap3.readthedocs.io/connections.html

c = Connection(s, user=admin_username, password=admin_password, auto_referrals=False) 
+0

我試過,但我仍然得到同樣的結果:( – rajagopalx

+1

嗯......根據這一點,可能是不可避免的:https://mail.python.org/pipermail/python3-ldap /2014/000042.html這看起來有點愚蠢,還有一個'dereference_aliases'選項用於搜索,這可能會改變行爲,會嘗試將其設置爲'DEREF_NEVER'。 – ChadSikorra

相關問題