python+ldap操作
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python+ldap操作相关的知识,希望对你有一定的参考价值。
参考技术A from ldap3 import Server, Connection, ALL, MODIFY_ADD, MODIFY_REPLACE, MODIFY_DELETEclass Connect_ldap:
def __init__(self): #初始化
self.server ='10.10.10.10' #ldap服务器地址
self.user ='cn=Directory Manager' # Bind DN or user
self.base_dn ='dc=bizenit,dc=com' #base_dn
self.pwd ='XXXXXXX' #密码
self.port =5389 #端口
def connect(self): #连接ldap服务
server = Server(host=self.server, port=self.port, get_info=ALL)
conn = Connection(server=server, user=self.user, password=self.pwd, auto_bind=True)
return conn
def add_user(self): #添加条目
i =1
j =17300000001
conn =self.connect()
while i <220000:
mail =str(j) +"@mail.com"
print(conn.add("uid=username%s,ou=Internal,ou=People,dc=bizenit,dc=com" % (i),
object_class=['top', 'inetOrgPerson', 'person', 'organizationalPerson',
'smart-extends', 'customized-extends'], #类型
attributes='cn':"username%s" % (i), 'smart-type':"E1",
'departmentNumber':"D1155073", 'mobile': j,
'smart-status':1, 'mail': mail, 'telephoneNumber': j,
'userPassword':"smart123")) #属性
i +=1
j +=1
def delete_user(self): #删除条目
i =1
conn =self.connect()
while i <220000:
print(conn.delete("uid=username%s,ou=Internal,ou=People,dc=bizenit,dc=com" % (i)))
i +=1
def modify_user(self): #修改条目
i =793123
j =17300793123
conn =self.connect()
while i <1000000:
conn.modify("uid=username%s,ou=Internal,ou=People,dc=bizenit,dc=com" % (i),
"objectClass": [(MODIFY_ADD, ['customized-extends', 'smart-extends'])])
mail =str(j) +"@mail.com"
a = conn.modify("uid=sidl%s,ou=Internal,ou=People,dc=bizenit,dc=com" % (i),
"departmentNumber": [(MODIFY_ADD, ['D1155073'])], "smart-type": [(MODIFY_ADD, ["E1"])],
"telephoneNumber": [(MODIFY_ADD, [j])], "userPassword": [(MODIFY_ADD, ["smart123"])],
"smart-status": [(MODIFY_ADD, [1])], "mail": [(MODIFY_ADD, [mail])])
print(a)
print(i)
i +=1
j +=1
def modify_dn_user(self): #移动条目
i =1
j =17300000001
conn =self.connect()
print(conn.modify_dn("uid=username%s,ou=People,dc=bizenit,dc=com" % (i), 'uid=username%s' % (i),
new_superior="ou=Internal,ou=People,dc=bizenit,dc=com"))
i +=1
j +=1
if __name__ =="__main__":
a = Connect_ldap()
a.add_user()
python--安装ldap报错
Running setup.py install for python-ldap ... error
ERROR: Command errored out with exit status 1:
command: /usr/local/python3/bin/python3.7 -u -c ‘import sys, setuptools, tokenize; sys.argv[0] = ‘"‘"‘/tmp/pip-install-6qi664ta/python-ldap/setup.py‘"‘"‘; __file__=‘"‘"‘/tmp/pip-install-6qi664ta/python-ldap/setup.py‘"‘"‘;f=getattr(tokenize, ‘"‘"‘open‘"‘"‘, open)(__file__);code=f.read().replace(‘"‘"‘ ‘"‘"‘, ‘"‘"‘ ‘"‘"‘);f.close();exec(compile(code, __file__, ‘"‘"‘exec‘"‘"‘))‘ install --record /tmp/pip-record-em2qy1ae/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/python3/include/python3.7m/python-ldap
cwd: /tmp/pip-install-6qi664ta/python-ldap/
Complete output (81 lines):
running install
running build
running build_py
creating build
creating build/lib.linux-x86_64-3.7
copying Lib/ldapurl.py -> build/lib.linux-x86_64-3.7
copying Lib/ldif.py -> build/lib.linux-x86_64-3.7
creating build/lib.linux-x86_64-3.7/ldap
copying Lib/ldap/async.py -> build/lib.linux-x86_64-3.7/ldap
copying Lib/ldap/syncrepl.py -> build/lib.linux-x86_64-3.7/ldap
copying Lib/ldap/constants.py -> build/lib.linux-x86_64-3.7/ldap
copying Lib/ldap/resiter.py -> build/lib.linux-x86_64-3.7/ldap
copying Lib/ldap/pkginfo.py -> build/lib.linux-x86_64-3.7/ldap
copying Lib/ldap/cidict.py -> build/lib.linux-x86_64-3.7/ldap
copying Lib/ldap/functions.py -> build/lib.linux-x86_64-3.7/ldap
copying Lib/ldap/modlist.py -> build/lib.linux-x86_64-3.7/ldap
copying Lib/ldap/asyncsearch.py -> build/lib.linux-x86_64-3.7/ldap
copying Lib/ldap/sasl.py -> build/lib.linux-x86_64-3.7/ldap
copying Lib/ldap/filter.py -> build/lib.linux-x86_64-3.7/ldap
copying Lib/ldap/dn.py -> build/lib.linux-x86_64-3.7/ldap
copying Lib/ldap/compat.py -> build/lib.linux-x86_64-3.7/ldap
copying Lib/ldap/ldapobject.py -> build/lib.linux-x86_64-3.7/ldap
copying Lib/ldap/__init__.py -> build/lib.linux-x86_64-3.7/ldap
copying Lib/ldap/logger.py -> build/lib.linux-x86_64-3.7/ldap
creating build/lib.linux-x86_64-3.7/ldap/controls
copying Lib/ldap/controls/readentry.py -> build/lib.linux-x86_64-3.7/ldap/controls
copying Lib/ldap/controls/openldap.py -> build/lib.linux-x86_64-3.7/ldap/controls
copying Lib/ldap/controls/vlv.py -> build/lib.linux-x86_64-3.7/ldap/controls
copying Lib/ldap/controls/libldap.py -> build/lib.linux-x86_64-3.7/ldap/controls
copying Lib/ldap/controls/simple.py -> build/lib.linux-x86_64-3.7/ldap/controls
copying Lib/ldap/controls/sss.py -> build/lib.linux-x86_64-3.7/ldap/controls
copying Lib/ldap/controls/pwdpolicy.py -> build/lib.linux-x86_64-3.7/ldap/controls
copying Lib/ldap/controls/deref.py -> build/lib.linux-x86_64-3.7/ldap/controls
copying Lib/ldap/controls/__init__.py -> build/lib.linux-x86_64-3.7/ldap/controls
copying Lib/ldap/controls/psearch.py -> build/lib.linux-x86_64-3.7/ldap/controls
copying Lib/ldap/controls/sessiontrack.py -> build/lib.linux-x86_64-3.7/ldap/controls
copying Lib/ldap/controls/ppolicy.py -> build/lib.linux-x86_64-3.7/ldap/controls
copying Lib/ldap/controls/pagedresults.py -> build/lib.linux-x86_64-3.7/ldap/controls
creating build/lib.linux-x86_64-3.7/ldap/extop
copying Lib/ldap/extop/dds.py -> build/lib.linux-x86_64-3.7/ldap/extop
copying Lib/ldap/extop/__init__.py -> build/lib.linux-x86_64-3.7/ldap/extop
creating build/lib.linux-x86_64-3.7/ldap/schema
copying Lib/ldap/schema/subentry.py -> build/lib.linux-x86_64-3.7/ldap/schema
copying Lib/ldap/schema/tokenizer.py -> build/lib.linux-x86_64-3.7/ldap/schema
copying Lib/ldap/schema/__init__.py -> build/lib.linux-x86_64-3.7/ldap/schema
copying Lib/ldap/schema/models.py -> build/lib.linux-x86_64-3.7/ldap/schema
creating build/lib.linux-x86_64-3.7/slapdtest
copying Lib/slapdtest/__init__.py -> build/lib.linux-x86_64-3.7/slapdtest
copying Lib/slapdtest/_slapdtest.py -> build/lib.linux-x86_64-3.7/slapdtest
running egg_info
writing Lib/python_ldap.egg-info/PKG-INFO
writing dependency_links to Lib/python_ldap.egg-info/dependency_links.txt
writing requirements to Lib/python_ldap.egg-info/requires.txt
writing top-level names to Lib/python_ldap.egg-info/top_level.txt
reading manifest file ‘Lib/python_ldap.egg-info/SOURCES.txt‘
reading manifest template ‘MANIFEST.in‘
no previously-included directories found matching ‘Doc/.build‘
writing manifest file ‘Lib/python_ldap.egg-info/SOURCES.txt‘
creating build/lib.linux-x86_64-3.7/slapdtest/certs
copying Lib/slapdtest/certs/README -> build/lib.linux-x86_64-3.7/slapdtest/certs
copying Lib/slapdtest/certs/ca.conf -> build/lib.linux-x86_64-3.7/slapdtest/certs
copying Lib/slapdtest/certs/ca.pem -> build/lib.linux-x86_64-3.7/slapdtest/certs
copying Lib/slapdtest/certs/client.conf -> build/lib.linux-x86_64-3.7/slapdtest/certs
copying Lib/slapdtest/certs/client.key -> build/lib.linux-x86_64-3.7/slapdtest/certs
copying Lib/slapdtest/certs/client.pem -> build/lib.linux-x86_64-3.7/slapdtest/certs
copying Lib/slapdtest/certs/gencerts.sh -> build/lib.linux-x86_64-3.7/slapdtest/certs
copying Lib/slapdtest/certs/gennssdb.sh -> build/lib.linux-x86_64-3.7/slapdtest/certs
copying Lib/slapdtest/certs/server.conf -> build/lib.linux-x86_64-3.7/slapdtest/certs
copying Lib/slapdtest/certs/server.key -> build/lib.linux-x86_64-3.7/slapdtest/certs
copying Lib/slapdtest/certs/server.pem -> build/lib.linux-x86_64-3.7/slapdtest/certs
running build_ext
building ‘_ldap‘ extension
creating build/temp.linux-x86_64-3.7
creating build/temp.linux-x86_64-3.7/Modules
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -DHAVE_SASL -DHAVE_TLS -DHAVE_LIBLDAP_R -DHAVE_LIBLDAP_R -DLDAPMODULE_VERSION=3.2.0 -DLDAPMODULE_AUTHOR=python-ldap project -DLDAPMODULE_LICENSE=Python style -IModules -I/usr/local/python3/include/python3.7m -c Modules/LDAPObject.c -o build/temp.linux-x86_64-3.7/Modules/LDAPObject.o
In file included from Modules/LDAPObject.c:8:0:
Modules/constants.h:7:18: 致命错误:lber.h:没有那个文件或目录
#include "lber.h"
^
编译中断。
error: command ‘gcc‘ failed with exit status 1
----------------------------------------
ERROR: Command errored out with exit status 1: /usr/local/python3/bin/python3.7 -u -c ‘import sys, setuptools, tokenize; sys.argv[0] = ‘"‘"‘/tmp/pip-install-6qi664ta/python-ldap/setup.py‘"‘"‘; __file__=‘"‘"‘/tmp/pip-install-6qi664ta/python-ldap/setup.py‘"‘"‘;f=getattr(tokenize, ‘"‘"‘open‘"‘"‘, open)(__file__);code=f.read().replace(‘"‘"‘ ‘"‘"‘, ‘"‘"‘ ‘"‘"‘);f.close();exec(compile(code, __file__, ‘"‘"‘exec‘"‘"‘))‘ install --record /tmp/pip-record-em2qy1ae/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/python3/include/python3.7m/python-ldap Check the logs for full command output.
在执行pip3 install python-ldap和pip3 install django-auth-ldap的时候报错如上,你以为ldap的模块不是这样下载的吗?于是用yum install,so? 解决办法很简单 1、yum install openldap-devel 2、执行上面的两条下载命令即可
以上是关于python+ldap操作的主要内容,如果未能解决你的问题,请参考以下文章
python :通过Ldap 3 模块查询AD 组和 AD 用户名单