簡體   English   中英

python-ldap無法在打開的服務器上執行任何基本搜索查詢

[英]python-ldap unable to do any basic search queries on open server

我一直在嘗試進行一些基本的搜索查詢,但是無論如何我都無法連接到開放的LDAP服務器。 我嘗試了幾台服務器,但都沒有工作。 我使用Apache Directory Studio來確保該關鍵字在那里,但是無論哪種方式都沒有作用。 我嘗試了來自不同來源的各種不同代碼。 這是我使用的第一個: https : //www.linuxjournal.com/article/6988

import ldap


keyword = "boyle"
def main():
    server = "ldap.forumsys.com"
    username = "cn=read-only-admin,dc=example,dc=com"
    password = "password"

    try:
        l = ldap.open(server)
        l.simple_bind_s(username,password)
        print "Bound to server . . . "
        l.protocol_version = ldap.VERSION3
        print "Searching . . ."
        mysearch (l,keyword)

    except ldap.LDAPError:
        print "Couldnt connect"

def mysearch(l, keyword):

    base = ""
    scope = ldap.SCOPE_SUBTREE
    filter = "cn=" + "*" + keyword + "*"

    retrieve_attributes = None

    count = 0
    result_set = []
    timeout = 0
    try:
        result_id = l.search(base, scope, filter, retrieve_attributes)

        while l != 1:
            result_id = l.search(base, scope,filter, retrieve_attributes)
            result_type, result_data = l.result(result_id, timeout)
            if result_data == []:
                break
            else:
                if result_type == ldap.RES_SEARCH_ENTRY:
                    result_set.append(result_data)

        if len (result_set = 0):
            print "No Results"

        for i in range (len(result_set)):
            for entry in result_set[i]:
                try:
                    name = entry[1]['cn'][0]
                    mail = entry[1]['mail'][0]
                    #phone = entry[1]['telephonenumber'][0]
                    #desc = entry[1]['description'][0]
                    count = count + 1
                    print name + mail
                except:
                    pass
    except ldap.LDAPError, error_message:
        print error_message

main()

每次我運行該程序時,都會收到錯誤{'desc':u“沒有這樣的對象”}

我也試過了

import ldap

try:

  l = ldap.open("ldap.example.com")


except ldap.LDAPError, e:
  print e


base_dn = "cn=read-only-admin,dc=example,dc=com"

search_scope = ldap.SCOPE_SUBTREE
retrieve_attributes = None
search_filter = "uid=myuid"

try:
  l_search = l.search(base_dn, search_scope, search_filter, retrieve_attributes)
  result_status, result_data = l.result(l_search, 0)
  print result_data
except ldap.LDAPError, e:
  print e

此錯誤為{'desc':u“無法聯系LDAP服務器”}

我花了大約5個小時試圖解決這個問題。 如果你們能給我一些建議,我將不勝感激。 謝謝。

里面有幾件偽造的東西。

我只會評論您的第一個代碼示例,因為具有該公共LDAP服務器的任何人都可以使用它。

l = ldap.open(服務器)

多年來,不贊成使用函數ldap.open()。 您應該將函數ldap.initialize()與LDAP URI用作參數,而不是像這樣:

l = ldap.initialize("ldap://ldap.forumsys.com")

l_search = l.search(..)

這是異步方法,僅返回基礎OpenLDAP C API(libldap)的消息ID(int)。 如果要檢索LDAP服務器返回的擴展控件以及搜索結果,則需要它。 那是你要的嗎?

作為初學者,您可能想使用更簡單的方法LDAPObject.search_s() ,該方法立即返回(DN,條目)2元組的列表。

另請參閱: python-ldap-發送LDAP請求

而l!= 1

這根本沒有任何意義,因為l是您的LDAPObject實例(LDAP連接對象)。 請注意,如果LDAPObject.search()從OpenLDAP的libldap獲取一個Integer錯誤代碼,則會引發異常。 無需在此級別執行C樣式錯誤檢查。

filter =“ cn =” +“ ” +關鍵字+“

如果可以任意輸入關鍵字,則很容易受到LDAP注入攻擊 不要那樣做

要將任意輸入添加到LDAP過濾器,請使用函數ldap.filter.escape_filter_chars()正確轉義特殊字符。 另外,請避免使用變量名稱過濾器,因為它是內置Python函數的名稱,並將過濾器正確地括在括號中。

更好的例子:

ldap_filter = "(cn=*%s*)" % (ldap.filter.escape_filter_chars(keyword))

基本=“”

您必須使用的正確搜索基礎是:

base =“ dc = example,dc = com”

否則,將引發ldap.NO_SUCH_OBJECT

因此,這是一個完整的示例:

import pprint

import ldap
from ldap.filter import escape_filter_chars

BINDDN = "cn=read-only-admin,dc=example,dc=com"
BINDPW = "password"
KEYWORD = "boyle"

ldap_conn = ldap.initialize("ldap://ldap.forumsys.com")
ldap_conn.simple_bind_s(BINDDN, BINDPW)

ldap_filter = "(cn=*%s*)" % (ldap.filter.escape_filter_chars(KEYWORD))

ldap_results = ldap_conn.search_s(
    "dc=example,dc=com",
    ldap.SCOPE_SUBTREE,
    ldap_filter,
)

pprint.pprint(ldap_results)

您的第一個查詢的基數無效。 應該是dc=example,dc=com ,而不是""

您的第二個查詢指向一個我無法聯系的未知主機。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM