简体   繁体   English

使用LDAP在Active Directory中查找用户-没有用户?

[英]Using LDAP to find Users in Active Directory - no Users?

Using (python-ldap 2.4.17.0) I am trying to get a list of all our users (username) from AD (Active Directory). 我正在尝试使用(python-ldap 2.4.17.0)从AD(Active Directory)获取所有用户(用户名)的列表。
When I execute the code below it only gives a list of Domain info, DNS servers, stuff about Exchange Servers - I can't seem to find anything to do with users from AD - even though when I go to the server and look at Active directory under the base/root node - there is a CN=Users, and all the good stuff I need underneath there!!! 当我执行下面的代码时,它仅提供域信息,DNS服务器以及有关Exchange Server的列表-我似乎找不到与AD中的用户有关的任何内容-即使当我转到服务器并查看“活动”时目录的根目录/根节点下-有一个CN = Users,所有我需要的好东西在下面!!!
But this does not reflect in the code. 但这并没有反映在代码中。 Anyone know why I can't see the Users via the code? 有人知道为什么我无法通过代码看到用户吗?

import ldap

def main():

    con=ldap.initialize('ldap://192.168.16.12:389')
    try:
        res =con.search_s("DC=mycompany,DC=local", ldap.SCOPE_SUBTREE)
        for dn, entry in res:
        print dn
except Exception, error:
    print error

RESULT 结果

DC=mycompany,DC=local
DC=ForestDnsZones,DC=mycompany,DC=local
DC=DomainDnsZones,DC=mycompany,DC=local
CN=Configuration,DC=mycompany,DC=local
CN=EXCH-FOOPLACE,CN=Public Folder Database 2,CN=Databases,CN=Exchange Administrative Group (FYDIBOHF23SPDLT),CN=Administrative Groups,CN=Mycompany,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=mycompany,DC=local
CN=EXCHANGE01,CN=Barplace-RegularMailBoxes,CN=Databases,CN=Exchange Administrative Group (FYDIBOHF23SPDLT),CN=Administrative Groups,CN=Mycompany,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=mycompany,DC=local
CN=EXCHANGE01,CN=Barplace-LargeMailBoxes,CN=Databases,CN=Exchange Administrative Group (FYDIBOHF23SPDLT),CN=Administrative Groups,CN=Mycompany,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=mycompany,DC=local
CN=EXCHANGE01,CN=Public Database,CN=Databases,CN=Exchange Administrative Group (FYDIBOHF23SPDLT),CN=Administrative Groups,CN=Mycompany,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=mycompany,DC=local
CN=EXCH-FOOPLACE,CN=Houston-Exchange02,CN=Databases,CN=Exchange Administrative Group (FYDIBOHF23SPDLT),CN=Administrative Groups,CN=Mycompany,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=mycompany,DC=local
CN=Schema,CN=Configuration,DC=mycompany,DC=local

Found out that you actually have to provide some kind of authorized user credentials before you query LDAP to get a fully comprehensive list of entities eg: 发现在查询LDAP以获得实体的完整列表之前,您实际上必须提供某种授权的用户凭证。例如:

con=ldap.initialize('ldap://192.168.16.12:38')

user_dn = r"Administrator@foo.com"
password = "bar"

criteria = "(&(objectClass=user)(sAMAccountName=username))"
attributes = ['displayName', 'company']

try:
    con.simple_bind_s(user_dn, password)
    res =con.search_s("CN=Users,DC=foo,DC=com", ldap.SCOPE_SUBTREE,'(objectClass=User)')
    for dn, entry in res:
        print dn
except Exception, error:
    print error

The answer is similar to @Vidar, but slightly expanded: 答案类似于@Vidar,但略有扩展:

import ldap

l = ldap.initialize("ldap://ldap.example.com")
try:
    l.protocol_version = ldap.VERSION3
    l.set_option(ldap.OPT_REFERRALS, 0)

    bind = l.simple_bind_s("me@example.com", "password")

    base = "dc=example, dc=com"
    criteria = "(&(objectClass=user)(sAMAccountName=username))"
    attributes = ['displayName', 'company']
    result = l.search_s(base, ldap.SCOPE_SUBTREE, criteria, attributes)

    results = [entry for dn, entry in result if isinstance(entry, dict)]
    print results

finally:
    l.unbind()

(c) https://rosettacode.org/wiki/Active_Directory/Search_for_a_user#Python (c) https://rosettacode.org/wiki/Active_Directory/Search_for_a_user#Python

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM