Created
July 18, 2020 12:50
-
-
Save pablospizzamiglio/1a8cb592a221cd9a249e09d8c8f40a86 to your computer and use it in GitHub Desktop.
Trying to connect to Active Directory with Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ssl | |
# pip install ldap3 | |
from ldap3 import ALL, NTLM, Connection, Server, Tls | |
host = "ldap-server.domain.com" | |
domain_name = "domain" | |
com = "com" | |
username = "username" | |
password = "password" | |
tls_configuration = Tls(validate=ssl.CERT_REQUIRED, version=ssl.PROTOCOL_TLSv1) | |
server = Server(host, use_ssl=True, tls=tls_configuration, get_info=ALL) | |
with Connection( | |
server, f"{domain_name}\\{username}", password, authentication=NTLM | |
) as conn: | |
conn.search( | |
f"dc={domain_name},dc={com}", | |
f"(&(objectclass=organizationalPerson)(cn={username}))", | |
attributes=["c", "cn", "displayName", "userPrincipalName"], | |
) | |
entry = conn.entries[0] | |
print(entry) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment