2010-06-16 65 views
0

我想用python查詢LDAP服務器,它總是返回我沒有結果。任何人都可以幫我找到我的Python代碼有什麼問題?它運行良好,沒有任何異常,並且它總是沒有結果。我玩過像「cn = partofmyname」的過濾器,但沒有運氣。 感謝您的幫助使用Python的LDAP查詢:總是沒有結果

import ldap 



try: 
    l = ldap.open("server") 
    l.protocol_version = ldap.VERSION3 
    l.set_option(ldap.OPT_REFERRALS, 0) 
    output =l.simple_bind("cn=username,cn=Users,dc=domian, dc=net",'password$R') 
    print output 
except ldap.LDAPError, e: 
    print e 

baseDN = "DC=domain,DC=net" 
searchScope = ldap.SCOPE_SUBTREE 
## retrieve all attributes - again adjust to your needs - see documentation for more options 
retrieveAttributes = None 

Filter = "(&(objectClass=user)(sAMAccountName=myaccount))" 

try: 
    ldap_result_id = l.search(baseDN, searchScope, Filter, retrieveAttributes) 
    print ldap_result_id 
    result_set = [] 
    while 1: 
     result_type, result_data = l.result(ldap_result_id, 0) 
     if len(result_data) == 0: 
      print 'no reslut' 
      break 
     else: 
      for i in range(len(result_set)): 
       for entry in result_set[i]: 
        try: 
         name = entry[1]['cn'][0] 
         email = entry[1]['mail'][0] 
         phone = entry[1]['telephonenumber'][0] 
         desc = entry[1]['description'][0] 
         count = count + 1 
         print "%d.\nName: %s\nDescription: %s\nE-mail: %s\nPhone: %s\n" %\ 
           (count, name, desc, email, phone) 
        except: 
         pass 
         ## here you don't have to append to a list 
         ## you could do whatever you want with the individual entry 
      #if result_type == ldap.RES_SEARCH_ENTRY: 
      # result_set.append(result_data) 
      # print result_set 
except ldap.LDAPError, e: 
    print e 
l.unbind() 
+0

我不知道 - 由於某種原因,我之前的編輯首選「ReSlut」稱號http://stackoverflow.com/posts/3057039/revisions它讓我。 – 2010-06-16 20:48:38

+0

如果我正確記得,python'ldap'模塊還可以打開/增加路由到std輸出的調試信息的詳細程度。 – ChristopheD 2010-06-16 21:19:05

回答

2

我發現我的問題。

simple_bind("cn=username,cn=Users,dc=domian, dc=net",'password$R') 

應該

simple_bind("domain/username",'password$R') 
+1

不知道它是否重要,但在第一行*域*是mispelt,應該是'dc = domain'(這個想法可能是兩種語法都有效)。 – 2011-01-18 18:14:25

+0

這隻適用於Active Directory。以前的版本應該可以在所有LDAP目錄中工作。如果您需要兼容性,請勿使用此選項。 – Davisein 2015-03-11 12:34:07

0

我會強烈建議你使用Wireshark的(www.wireshark.org),看看發生了什麼事在協議級檢查網絡流量。此外,請獲取諸如Softerra LDAP瀏覽器2.6(免費版http://www.ldapbrowser.com/download.htm)之類的工具來檢查A/D服務器和目錄組織。

如果仍有問題,請發佈您使用這些工具查找的摘要。