# `apt install python3-ldap` or `pip install python-ldap` from getpass import getpass import ldap # This is needed for self-signed certs or when you don't have the CA locally # But it makes the client less secure. Only enable the following line # if you really need to (perhaps for troubleshooting). # ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER) # Use ldap:// or ldaps:// con = ldap.initialize('ldap://ldap.devdungeon.com') # If you are using ldaps:// you don't need to StartTLS too try: con.start_tls_s() print("StartTLS initialized properly.") except ldap.LDAPError as e: print("Could not start TLS.", e) exit(1) try: print("Attempting to bind") password = getpass() con.bind_s('cn=dano,dc=devdungeon,dc=com', password) except ldap.INVALID_CREDENTIALS: print('Invalid credentials') except ldap.INVALID_DN_SYNTAX: print('Invalid distinguished name.') print(f"I am bound as: {con.whoami_s()}")