Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Ldap3 Library how to get users and departments in AD

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to use the Ldap3 library to get users and departments in AD. I think it is very practical, so I share it with you. I hope you can get something after reading this article.

Today, I'll show you how to use the Ldap3 library to get users and departments (OU) in AD.

#! / usr/bin/env python#-*-coding: utf-8-*-import jsonfrom ldap3 import ALL_ATTRIBUTES# Note: if you want to use tls (secure connection) in the ldap3 library, you need to install and configure the certificate service in the ad service before you can connect through tls. Otherwise, LDAPSocketOpenError will be reported during the connection test (if the unable to open socket'# is to modify the account password and activate the account. Will report an error: "WILL_NOT_PERFORM" from ldap3 import Connection, NTLM, Serverfrom ldap3 import MODIFY_REPLACEserver1 = Server ("adtest.com", port=636, use_ssl=True, get_info=ALL) Connect_timeout=5) LDAP_SERVER_POOL = [server1] SERVER_USER = 'adtest\\ administrator'SERVER_PASSWORD = "XXXXXXX" class AD (object):''AD user Action 'def _ _ init__ (self):' 'initialization' self.conn = Connection (# configure server connection parameters server=LDAP_SERVER_POOL, auto_bind=True, authentication=NTLM # this read_only=False needs to be configured to connect to Windows AD. # data modification is prohibited: True user=SERVER_USER,# administrator account password=SERVER_PASSWORD,) self.leaved_base_dn = 'ou=Leaved,dc=adtest,dc=intra'# termination account OU self.active_base_dn =' ou= test department, dc=adtest Dc=intra'# regular employee account OU self.search_filter ='(objectclass=user)'# only get [user] object self.ou_search_filter ='(objectclass=organizationalUnit)'# only get [OU] object def users_get (self):''get all users' self.conn.search (search_base=self.active_base_dn,search_filter=self.search_filter Attributes=ALL_ATTRIBUTES) res = self.conn.response_to_json () res = json.loads (res) ['entries'] return res def OU_get (self):' 'get all OU''' self.conn.search (search_base=self.active_base_dn,search_filter=self.ou_search_filter) Attributes=ALL_ATTRIBUTES) res = self.conn.response_to_json () res = json.loads (res) ['entries'] return res

The above is how to use the Ldap3 library to get users and departments in AD. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report