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 in python

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

Share

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

This article introduces the relevant knowledge of "how to use the Ldap3 library in python". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Today, I will show you how to use the Ldap3 library to delete users and departments (OU) in AD, and how to encapsulate an account password authentication method.

#! / usr/bin/env python

#-*-coding: utf-8-*-

Import json

From ldap3 import ALL_ATTRIBUTES

# Note: if you want to use tls (secure connection) in ldap3 library, you need ad service to install and configure certificate service first.

# to connect via tls, otherwise LDAPSocketOpenError ('unable to open socket') will be reported during the connection test

# if the account password is changed and the account is activated, an error will be reported: "WILL_NOT_PERFORM"

From ldap3 import Connection, NTLM, Server

From 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)'# get only [user] object self.ou_search_filter ='(objectclass=organizationalUnit)'# only get [OU] object def check_credentials (username Password): "" user authentication interface # "" ldap_user ='\ {} @ adtest.intra'.format (username) server = Server ('adtest.intra', use_ssl=True) connection = Connection (server, user=ldap_user, password=password, authentication=NTLM) try: logger.info ("username:%s") Res:% s "% (username,connection.bind ()) return connection.bind () except: logger.warning (" username:%s; res:% s "% (username,connection.bind () return False finally: connection.closed

Def del_obj (self,DN):''delete user or Department: param DN:: return:True' res = self.conn.delete (dn=DN) return res "how to use the Ldap3 Library in python" ends here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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