【问题标题】:How to read the contents of active directory using python-ldap?如何使用 python-ldap 读取活动目录的内容?
【发布时间】:2012-11-16 04:40:36
【问题描述】:

我的脚本是这样的:

import ldap, sys
server = 'ldap://my_server'
l = ldap.initialize(server)
dn="myname@mydomain"
pw = "password"
l.simple_bind_s(dn,pw)
ldap.set_option(ldap.OPT_REFERRALS,0)
print "valid"

我在 Windows 上使用 Python 2.7。

有什么方法可以读取或获取活动目录的内容吗?

【问题讨论】:

    标签: python-2.7 python-ldap


    【解决方案1】:

    您还可以使用win32com.client 做很多事情(我很难找到相关文档)。例如,我需要解析知道他的ADS_NAME_TYPE_NT4 格式名称(doman\jonjoe)的用户电子邮件。

    首先需要将其转换为ADS_NAME_TYPE_1779格式(CN=Jeff Smith,CN=users,DC=Fabrikam,DC=com):

    name_resolver = win32com.client.Dispatch(dispatch='NameTranslate')
    name_resolver.Set(3, 'domain\\jonjoe')
    ldap_query = 'LDAP://{}'.format(name_resolver.Get(1))
    

    一旦你有了,你可以简单地打电话给GetObject():

    ldap = win32com.client.GetObject(ldap_query)
    print(ldap.Get('mail'))
    

    使用 Python 3.2.5 测试

    【讨论】:

    • 这个库是否也可以解析来自通过单向信任连接的不同林的外部安全主体的域?
    • @AndrzejBobak 它基本上只是包装了 WinAPI,所以如果你可以用 WinAPI 编写它,你可以使用win32* python 库重写它。
    【解决方案2】:

    你应该真的需要阅读 python-ldap http://www.python-ldap.org/docs.shtml 的文档

    You have a connection in your variable l, then you can do this.
    
    l.con_search_s('dc=your,dc=base,dc=dit', ldap.SCOPE_SUBTREE, 'uid=*', ['uid', 'uidnumber'])
    
    The above code, goint to search in to all the uid's entrys, for if entry, is going to get the uid and the uidnumbre attributes.
    

    【讨论】:

      猜你喜欢
      • 2021-08-07
      • 2015-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-13
      相关资源
      最近更新 更多