Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save gmirsky/79c7e8babf2f3a6b9897f9fed143be10 to your computer and use it in GitHub Desktop.

Select an option

Save gmirsky/79c7e8babf2f3a6b9897f9fed143be10 to your computer and use it in GitHub Desktop.
python_active_directory_authenticate.py
import ldap3
import json
server_password='Passw0rds123!'
server_ip='172.16.10.50'
server = ldap3.Server(
server_ip,
port=636,
use_ssl=True
)
server_connection = ldap3.Connection(
server,
'CN=ldap_bind_account,OU=1_Service_Accounts,OU=0_Users,DC=TG,DC=LOCAL',
server_password,
auto_bind=True
)
print(server_connection)
"""
ldaps://172.16.10.50:636 - ssl - user: CN=ldap_bind_account,OU=1_Service_Accounts,OU=0_Users,DC=TG,DC=LOCAL - not lazy - bound - open - <local: 192.168.1.12:54408 - remote: 172.16.10.50:636> - tls not started - listening - SyncStrategy - internal decoder
"""
# Searching for user objects
server_connection.search(
'DC=TG,DC=Local',
'(&(objectclass=user)(sAMAccountName=bobtest))'
)
"""
True
"""
response = json.loads(
server_connection.response_to_json()
)
print(response)
"""
{'entries': [{'attributes': {}, 'dn': 'CN=Bob Test,OU=1_Standard_Users,OU=0_Users,DC=TG,DC=LOCAL'}]}
"""
# Query group membership
server_connection.search('DC=TG,DC=Local',
'(&(objectclass=user)(sAMAccountName=bobtest))',
attributes=['memberOf']
)
"""
True
"""
response = json.loads(
server_connection.response_to_json()
)
print(response)
"""
{'entries': [{'attributes': {'memberOf': ['CN=DL_VRA_ADMIN,OU=0_Groups,DC=TG,DC=LOCAL']}, 'dn': 'CN=Bob Test,OU=1_Standard_Users,OU=0_Users,DC=TG,DC=LOCAL'}]}
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment