热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Python使用ldap3操作微软AD

对于client连接ldapserver的策略,ldap3提供了4种选择,可以通过client_strategy设置Connectionobject应用哪种策略:l SYNCl ASYNCl 

对于client连接ldap server的策略,ldap3提供了4种选择,可以通过client_strategy设置Connection object应用哪种策略:

l  SYNC

l  ASYNC

l  RESTARTABLE

l  REUSABLE

同步策略(SYNC, RESTARTABLE),所有的ldap操作返回True/False

异步策略(ASYNC, REUSABLE)返回一个msgid(一个数值),异步策略发送请求后不用等待响应,需要响应的时候直接使用get_response(message_id)获取结果。等待响应的超时时间可以通过get_responsetimeout参数指定,默认10s。如果使用get_request=True in the get_response(),同时会返回发送的请求字典。

 

建立连接:

Python使用ldap3操作微软AD

Python使用ldap3操作微软AD


Python使用ldap3操作微软AD

Python使用ldap3操作微软AD


建立Server对象时使用get_info=ldap3.ALL参数,建立Connection连接之后可以获取到server信息(匿名获取),从中可以获取到域名信息,域控计算机名,ldap server支持的ExtensionControl

建立Server时指定 active=True,建立连接前会先检查ldap server的可用性;active=5指定抛出 LDAPServerPoolExhaustedError异常之前重试的次数

 exhaust=True : 如果ldap server不时active,server将会从pool中移除。exhaust=10:设置为数值,表示认为server 10s不可达,则认为它为offline,

Python使用ldap3操作微软AD

When all servers in a pool are not available the strategy will wait for the number of seconds specified in ldap.

POOLING_LOOP_TIMEOUT before starting a new cycle. This defaults to 10 seconds.

The pool can have different HA strategies:

• FIRST: gets the first server in the pool, if ‘active’ is set to True gets the first available server

• ROUND_ROBIN: each time the connection is open the subsequent server in the pool is used. If active is set to

True unavailable servers will be discarded

• RANDOM: each time the connection is open a random server is chosen in the pool. If active is set to True

unavailable servers will be discarded

A server pool can be defined in different ways:

server1 = Server('server1')

server2 = Server('server2')

server3 = Server('server1', port=636, use_ssl=True)

• explicitly with Server objects in the init:

server_pool = ServerPool([server1, server2, server3], POOLING_STRATEGY_ROUND_

˓→ ROBIN, active=True, exhaust=True)

• explicitly with an add operation in the pool object:

server_pool = ServerPool(None, POOLING_STRATEGY_ROUND_ROBIN_ACTIVE)

server_pool.add(server1)

server_pool.add(server2)

server_pool.add(server3)

44 Chapter 1. Contents

ldap3 Documentation, Release 2.5

• implicitly directly in the Connection object init (passing a list of servers):

cOnn= Connection([server1, server2, server3]) # the ServerPool object is

˓→ defined with the default pooling strategy

Pools can be dynamically changed. You can add and remove Server objects from pools even if they are already used

in Connection:

server4 = Server('server2', port=636, use_ssl=True)

server_pool.remove(server2)

server_pool.add(server4)

Connections are notified of the change and can reopen the socket to the new server at next open() operation.

You can also save the schema and info in a json string:

json_info = server.info.to_json()

json_schema = server.schema.to_json()

or can have them saved on file:

server.info.to_file('server-info.json')

server.schema.to_file('server-schema.json')

to build a new server object with the saved json files you can retrieve them with:

from ldap3 import DsaInfo, SchemaInfo

dsa_info = DsaInfo.from_file('server-info.json')

schema_info = SchemaInfo.from_file('server-schema.json')

server = Server('hostname', dsa_info, schema_info)

 

ldap serverSchema数据库中存储了ldap server中的对象的已知类型信息,可以通过server.schema获取到(微软AD需要鉴权,匿名用户无法获取),里面存储了ldap server理解那些数据类型,同时也指定,哪些属性被ldap server中的对象支持

Python使用ldap3操作微软AD

使用鉴权用户连接ldap server后可以查看server.shema等高级别操作。查看当前鉴权用户信息。以下连接使用的不安全的连接,密码信息明文传输,可以被抓取。使用authentication=ldap3.NTLM的鉴权方式无法显示的看到鉴权信息。


Python使用ldap3操作微软AD

Python使用ldap3操作微软AD

Python使用ldap3操作微软AD

可以使用以下方式建立安全连接,2种方式都是建立TLS连接:

l  LDAP over TLS

l  the StartTLS extended operation     ##微软AD不支持

 

ldap查询

ldap查询基于search_basesearch_filterfilter是个表达式:

l  查询所有显示名叫John并且email以‘@example.org’结尾的用户:(&(givenName=John)(mail=*@example.org))

l  查询显示名为Jhon或者Fred并且邮箱以@example.org结尾的用户

(&

(|

(GivenName=Jhon)

(givenName=Fred)

)

( mail=*@example.org)

)

搜索search_base下的所有用户,默认search_scope='SUBTREE',没有指定请求任何attribute,只返回entriesdistinguished Name,请求成功(同步strategy)返回True,conn.entries获取查询到的结果:

conn.search(base_search,'(objectclass=person)')

conn.entries

Python使用ldap3操作微软AD

可以使用访问字典或者访问对象属性的方式访问从server上获取到的attribute值,有些属性不区分大小写,raw_values获取到的是从server返回的原始的值:



 

Python使用ldap3操作微软AD

Python使用ldap3操作微软AD

Python使用ldap3操作微软AD

 

返回的entry可以格式化为json字符串

Python使用ldap3操作微软AD

如果查询的属性的值为空,返回的entries中将不包含此属性,除非在Connection中指定return_empty_attributes=False,微软AD中貌似不起作用。

Python使用ldap3操作微软AD

ldap server进行search操作之后,Connection有以下属性可以访问:

Python使用ldap3操作微软AD


在AD上增加entry,第一个参数为增加的对象dn,第二个参数为object_class,指定创建的object的类型,第三个参数为object提供的个性化attribute:


 Python使用ldap3操作微软AD

域控支持的objectclass可以通过server.schema获取到,创建不同类型的objectclass支持哪些attribute可以通过server.schema.object_classes['user']方式获取到,大多数attribute在创建object的时候都是可选的,必选参数会单独列出:

Python使用ldap3操作微软AD

Python使用ldap3操作微软AD


重命名一个dn,利用modify_dn提供的参数new_superior=new_dn,还可以将dn从一个ou移动到另一个ou:

Python使用ldap3操作微软AD

Python使用ldap3操作微软AD

 

检查object的属性是否和给定值一样。

Python使用ldap3操作微软AD



推荐阅读
  • Java在运行已编译完成的类时,是通过java虚拟机来装载和执行的,java虚拟机通过操作系统命令JAVA_HOMEbinjava–option来启 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 在Kubernetes上部署JupyterHub的步骤和实验依赖
    本文介绍了在Kubernetes上部署JupyterHub的步骤和实验所需的依赖,包括安装Docker和K8s,使用kubeadm进行安装,以及更新下载的镜像等。 ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文详细介绍了PHP中与URL处理相关的三个函数:http_build_query、parse_str和查询字符串的解析。通过示例和语法说明,讲解了这些函数的使用方法和作用,帮助读者更好地理解和应用。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • imx6ull开发板驱动MT7601U无线网卡的方法和步骤详解
    本文详细介绍了在imx6ull开发板上驱动MT7601U无线网卡的方法和步骤。首先介绍了开发环境和硬件平台,然后说明了MT7601U驱动已经集成在linux内核的linux-4.x.x/drivers/net/wireless/mediatek/mt7601u文件中。接着介绍了移植mt7601u驱动的过程,包括编译内核和配置设备驱动。最后,列举了关键词和相关信息供读者参考。 ... [详细]
  • 本文介绍了如何使用C#制作Java+Mysql+Tomcat环境安装程序,实现一键式安装。通过将JDK、Mysql、Tomcat三者制作成一个安装包,解决了客户在安装软件时的复杂配置和繁琐问题,便于管理软件版本和系统集成。具体步骤包括配置JDK环境变量和安装Mysql服务,其中使用了MySQL Server 5.5社区版和my.ini文件。安装方法为通过命令行将目录转到mysql的bin目录下,执行mysqld --install MySQL5命令。 ... [详细]
author-avatar
大大炮
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有