0

I am trying to write a python script to validate if a username exists in a particular domain or not .

To query :
           username -- > anandabhis
           domain name --> example.com
Output : Successfully verified .

For this I have used python-ldap module to connect to LDAP server . But I am unable to proceed further even after reading lots of documentations.

import ldap
def test_login(self):
        domain = 'EXAMPLE'
        server = 'ldap-001.example.com'
        admin_username = 'admin'
        admin_password = 'secret-password'
        connection = ldap.initialize('ldap://{0}'.format(server))
        connection.protocol_version = 3
        connection.set_option(ldap.OPT_REFERRALS, 0)
        connection.simple_bind_s('{0}\{1}'.format(domain, admin_username), admin_password)
        search_username = 'anandabhis'
user46663
  • 11
  • 1
  • 3
  • 8

1 Answers1

1

A simple search for the sAMAccountName of the user should allow you to get the attributes of the user.

user_filter = '(sAMAccountName={})'.format(search_username)
base_dn = 'DC={},DC=com'.format(domain)
result = connection.search_s(base_dn, ldap.SCOPE_SUBTREE, user_filter)
print result
ARPT
  • 141
  • 4