Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Ldap3 Library in python

Shulou Source: shulou.com Published: 2022-06-02 04:16:10 10月05日 Update

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!

Tags: User account method department service configuration usage content password object location more knowledge will report test authentication practical security learning next Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux OPPO Reno Redmi Apple NVidia