2017-10-22 138 views
0

我想寫一個python腳本來驗證用戶名是否存在於特定的域中。查詢AD中用戶使用python

To query : 
      username -- > anandabhis 
      domain name --> example.com 
Output : Successfully verified . 

爲此,我使用python-ldap模塊連接到LDAP服務器。但即使閱讀了大量文件,我仍然無法繼續。

import ldap 
def test_login(self): 
     domain = 'EXAMPLE' 
     server = 'ldap-001.example.com' 
     admin_username = 'admin' 
     admin_password = 'secret-password' 
     connection = ldap.initialize('ldap://{0}'.format(server)) 
     connection.protocol_version = 3 
     connection.set_option(ldap.OPT_REFERRALS, 0) 
     connection.simple_bind_s('{0}\{1}'.format(domain, admin_username), admin_password) 
     search_username = 'anandabhis' 
+0

您必須立即進行搜索。 –

+0

@KlausD。我不知道如何搜索。你可以幫助我使用哪個功能嗎? – user46663

+0

[如何查找用戶是其成員的所有組的可能的副本? (LDAP)](https://stackoverflow.com/questions/40225230/how-to-find-all-the-groups-the-user-is-a-member-ldap) – thatrockbottomprogrammer

回答

0

簡單搜索用戶的sAMAccountName應允許您獲取用戶的屬性。

user_filter = '(sAMAccountName={})'.format(search_username) 
base_dn = 'DC={},DC=com'.format(domain) 
result = connection.search_s(base_dn, ldap.SCOPE_SUBTREE, user_filter) 
print result