简体   繁体   English

如何使用python-ldap读取活动目录的内容?

[英]How to read the contents of active directory using python-ldap?

My script is like this: 我的脚本是这样的:

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"

I am using Python 2.7 on windows. 我在Windows上使用Python 2.7。

Is there any method to read or get the contents of active directory? 有没有方法可以读取或获取活动目录的内容?

You can do quite a lot also using win32com.client (which I had trouble finding documentation for). 你也可以使用win32com.client做很多win32com.client (我找不到文档)。 For example I've needed to resolve user email knowing his ADS_NAME_TYPE_NT4 formatted name ( doman\\jonjoe ). 例如,我需要知道他的ADS_NAME_TYPE_NT4格式名称( doman\\jonjoe )来解析用户电子邮件。

First of all you need to convert it to ADS_NAME_TYPE_1779 format ( CN=Jeff Smith,CN=users,DC=Fabrikam,DC=com ): 首先,您需要将其转换为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))

Once you have that you can simply call GetObject() : 一旦你有了,你可以简单地调用GetObject()

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

Tested with Python 3.2.5 用Python 3.2.5测试

You should realy need to read the documentation of python-ldap http://www.python-ldap.org/docs.shtml 你应该真的需要阅读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.

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

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