Мне нужно выполнить следующую команду в python:
ldapsearch -H ldap://10.120.80.17:300 -x -LLL "uid=cich"
то я получаю это как результат в bash:
dn: employeeNumber=621,ou=Internal,ou=People,o=NSN
mail: [email protected]
objectClass: nsnEDPerson
objectClass: inetorgperson
objectClass: organizationalPerson
objectClass: person
objectClass: top
objectClass: dspswuser
objectClass: posixAccount
objectClass: shadowAccount
nsnSiteCode: 600906
gidNumber: 555
uid: cich
loginShell: /bin/bash
homeDirectory: /home/cich
nsnPrimaryEmailAddress: [email protected]
gecos: surname name
employeeNumber: 6216
cn: surname name
uidNumber: 6216
это то, что я пытался выполнить в python с помощью python-ldap lib, но это не работает, я получаю ошибку в последней строке с 'результатом'
import ldap
con = ldap.initialize('ldap://10.120.80.17:300')
ldap_base = "ou=Internal,ou=People,o=NSN"
query = "(uid=cich)"
result = con.search_s(ldap_base, ldap.SCOPE_SUBTREE, query)
Думаю, я не понимаю, что мне нужно добавить в ldap_base, не могли бы вы объяснить?
есть документация из библиотеки:
def search_ext(self,base,scope,filterstr=None,attrlist=None,attrsonly=0,serverctrls=None,clientctrls=None,timeout=-1,sizelimit=0):
"""
search(base, scope [,filterstr='(objectClass=*)' [,attrlist=None [,attrsonly=0]]]) -> int
search_s(base, scope [,filterstr='(objectClass=*)' [,attrlist=None [,attrsonly=0]]])
search_st(base, scope [,filterstr='(objectClass=*)' [,attrlist=None [,attrsonly=0 [,timeout=-1]]]])
search_ext(base,scope,[,filterstr='(objectClass=*)' [,attrlist=None [,attrsonly=0 [,serverctrls=None [,clientctrls=None [,timeout=-1 [,sizelimit=0]]]]]]])
search_ext_s(base,scope,[,filterstr='(objectClass=*)' [,attrlist=None [,attrsonly=0 [,serverctrls=None [,clientctrls=None [,timeout=-1 [,sizelimit=0]]]]]]])
Perform an LDAP search operation, with base as the DN of
the entry at which to start the search, scope being one of
SCOPE_BASE (to search the object itself), SCOPE_ONELEVEL
(to search the object's immediate children), or SCOPE_SUBTREE
(to search the object and all its descendants).
filter is a string representation of the filter to
apply in the search (see RFC 4515).
Each result tuple is of the form (dn,entry), where dn is a
string containing the DN (distinguished name) of the entry, and
entry is a dictionary containing the attributes.
Attributes types are used as string dictionary keys and attribute
values are stored in a list as dictionary value.
The DN in dn is extracted using the underlying ldap_get_dn(),
which may raise an exception of the DN is malformed.
If attrsonly is non-zero, the values of attrs will be
meaningless (they are not transmitted in the result).
The retrieved attributes can be limited with the attrlist
parameter. If attrlist is None, all the attributes of each
entry are returned.
serverctrls=None
clientctrls=None
The synchronous form with timeout, search_st() or search_ext_s(),
will block for at most timeout seconds (or indefinitely if
timeout is negative). A TIMEOUT exception is raised if no result is
received within the time.
The amount of search results retrieved can be limited with the
sizelimit parameter if non-zero.
"""
Аргумент base
- это база поиска, иногда называемая корнем поиска. Он определяет поддерево всего дерева информации каталога (DIT), в котором вы начинаете поиск. Он имеет то же значение, что и параметр командной строки -b
инструмента ldapsearch. Ваша локальная конфигурация (файл ldap.conf), вероятно, содержит для этого значение по умолчанию.
В большинстве случаев вы выбираете для этого запись верхнего уровня базы данных вашего LDAP-сервера. Если ваш сервер LDAP предоставляет доступ для чтения к корневой DSE, вы можете запросить «контексты именования» различных баз данных на вашем сервере LDAP следующим образом:
ldapsearch -H ldap://10.120.80.17:300 -x -LLL -b "" namingContexts
Не совсем ясно, действительно ли у вас есть проблемы с вашим скриптом Python или вам нужно только общее понимание.