使用Python脚本管理远程LDAP

2024-09-27 23:19:57 发布

您现在位置:Python中文网/ 问答频道 /正文

背景: 我正在开发一个API来集中用户创建和管理多个资源(例如Google Apps、Dropbox等)。 在linuxvm上,我开发了一个API和web接口,允许我(和我的共同管理员)验证和管理这些服务的用户帐户。 接下来我需要集成的是我们的活动目录,它托管在远程WindowsServer2008上。在

我一直在尝试使用pythonldap来连接和检索/修改信息,但是遇到了DIR_错误操作错误(尝试查询用户时)和命名违反错误(尝试添加用户时)的问题。在

*基于http://www.grotan.com/ldap/python-ldap-samples.html、stackoverflow问题和python ldap文档的代码 我认为有效的绑定代码:

import ldap
try:
    l = ldap.open("serverip")
    l.protocol_version = ldap.VERSION3  

    username = "myUserName@adtest.local"
    password  = "secret"

    result = l.simple_bind(username, password)
    print result

except ldap.LDAPError, e:
    print e

哪个打印: (97,[],1,[])

用户查询脚本: (根据文章的建议,尝试了不绑定,但收到“若要执行此操作,必须在连接上完成成功的绑定”。)

^{pr2}$

结果如下: (97,[],1,[]) {'info':'000020D6:SvcErr:DSID-031007DB,问题5012(DIR_ERROR),数据0\n','desc':'操作错误'}

添加用户脚本:(使用ldap)

import ldap
import ldap.modlist as modlist

# Open a connection
l = ldap.initialize("ldaps://serverIp:636/")

# Bind/authenticate with a user with apropriate rights to add objects
l.simple_bind_s("myUserName@adtest.local","secret")

# The dn of our new entry/object
dn="cn=test,dc=adtest,dc=local" 

# A dict to help build the "body" of the object
attrs = {}
attrs['objectclass'] = ['top','organizationalRole','simpleSecurityObject']
attrs['cn'] = 'test'
attrs['userPassword'] = 'aDifferentSecret'
attrs['description'] = 'test user'

# Convert our dict to nice syntax for the add-function using modlist-module
ldif = modlist.addModlist(attrs)

# Do the add-operation to the ldapserver
l.add_s(dn,ldif)

# Disconnect and free resources when done
l.unbind_s()

结果是: ldap.SERVER_关闭:{'info':'收到长度意外的TLS数据包。','desc':“无法联系LDAP服务器”}

*这使我认为端口可能是问题所在,因此我将initialize行改为l = ldap.initialize("ldap://serverIp:389/"),类似于其他两个脚本。在

现在我得到: ldap.NAMING_冲突:{'info':“00002099:nameer:DSID-0305109C,问题2005(命名冲突),数据0,最佳匹配:\n\t'dc=adtest,dc=local'\n”,'desc':'命名冲突'}

另外,我还把ou和uid添加到属性中,但是没有错误的改变。在

我做错了什么?或者我可以尝试做些什么? 谢谢您的帮助/建议!在

编辑:我检查了我的服务器,端口636被正确地设置为允许安全的LDAP流量,所以我不知道为什么会出现与普通LDAP不同的错误。 编辑2:我试着在添加脚本中更改以下行 dn=“cn=测试,dc=adtest.本地““

我得到的新输出(堆栈跟踪)是(我在中添加了print语句,以表明绑定实际上是在错误发生之前发生的):
(97,[],1,[])
回溯(最近一次呼叫):
文件“test2.py”,第21行,在<module>
l、 添加(dn,ldif)
文件“/usr/local/lib/python2.7/dist-packages/ldap/ldapobject.py“,第202行,添加
返回自我结果(msgid,all=1,超时=自身超时)
文件“/usr/local/lib/python2.7/dist-packages/ldap/ldapobject.py“,第465行,结果
响应类型,响应数据,响应消息ID=自身结果2(msgid,all,timeout)
文件“/usr/local/lib/python2.7/dist-packages/ldap/ldapobject.py“,第469行,结果2
响应类型,响应数据,响应消息,响应消息=自身结果3(msgid,all,timeout)
文件“/usr/local/lib/python2.7/dist-packages/ldap/ldapobject.py“结果行<476,t3/> resp_ctrl_classes=响应控制类
文件“/usr/local/lib/python2.7/dist-packages/ldap/ldapobject.py“,第483行,结果4
ldap_result=self.\u ldap_call(self.\l.result4,msgid,all,timeout,add\ctrls,add_intermediates,add_extop)
文件“/usr/local/lib/python2.7/dist-packages/ldap/ldapobject.py“,第106行,in_ldap_call
结果=func(*args,**kwargs)
ldap.REFERRAL:{'info':'推荐:\nldap://adtest.local/cn=test,华盛顿特区=adtest.本地','desc':'推荐'}


Tags: 文件用户pyaddlibpackagesusrlocal
1条回答
网友
1楼 · 发布于 2024-09-27 23:19:57

工作查询搜索!
贷方:
http://www.linuxjournal.com/article/6988?page=0,0

import ldap

def main():

    keyword = "user_query"

    try:
        l = ldap.open(serverIp)
        l.simple_bind_s("myUserName@adtest.local", "password")
        print "successfully bound to server.\n"

        print "Searching..\n"
        my_search(l,keyword)
    except ldap.LDAPError, e:
        print "Couldn't connect. %s " % e

def my_search(l, keyword):
    #Base is for the DN(Distinguised Name) of the entry where the search should start
    base = "cn=Users,dc=adtest,dc=local"
    #Scope has three options, SUBTREE searches all sub-folder/directories
    scope = ldap.SCOPE_SUBTREE
    #filter consists of a cn(common name) and keyword.
    #putting asterisks around our keyword will match anything containing the string
    f = "cn=" + "*" + keyword + "*"
    #determines which attributes to return. Returns all if set to "None"
    retrieve_attributes = None

    count = 0
    result_set = []
    timeout = 0
    result = l.search_s(base, scope, f, retrieve_attributes)
    print result[0][1].keys()
    try:
        result_id = l.search(base, scope, f, retrieve_attributes)
        while 1:
            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"
            return
        for i in range(len(result_set)):
            for entry in result_set[i]:
                try:
                    name = entry[1]['cn'][0]
                    count += 1
                    print str(count)+" "+name
                except:
                    pass
    except ldap.LDAPError, e:
        print e

if __name__=='__main__':
    main()

我修复了代码中的一个错误,但是仍然无法设置某些属性,因为LDAP使用纯文本,并且不允许在没有安全连接的情况下发送私有信息。 为了添加/修改用户密码信息和userAccountControl标志(启用用户),我使用端口636切换到ldap,通过添加Active Directory证书服务(*要求您重新启动服务器)在服务器上启用了该端口。
此外,您还需要包括'ldap.set_选项(ldap.OPT_X_TLS_需要证书,0)行,然后初始化。在

工作添加用户
贷方:
how to set lockoutTime and password of a user of Active Directory

^{pr2}$

相关问题 更多 >

    热门问题