Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- LDAP example for VSB-TUO
- You need python-ldap package: pip install python-ldap (tested on version 3.1.0)
- '''
- import getpass
- import ldap
- if __name__ == '__main__':
- ldap.set_option(ldap.OPT_REFERRALS,0)
- ldap.protocol_version = 3
- ldap_server='ldaps://ldap.vsb.cz'
- username = input('username: ')
- password= getpass.getpass('password: ')
- # VSB specific user context
- trailing_context = username[-1]
- # the following is the user_dn format provided by the ldap server
- user_dn = 'cn=' + username
- # adjust this to your base dn for searching
- base_dn = 'ou=USERS,o=VSB'
- connect = ldap.initialize(ldap_server)
- search_filter = 'cn=' + username
- listing = connect.search_s(base_dn, ldap.SCOPE_SUBTREE, user_dn, [])
- if len(listing) == 0:
- print('User not found')
- else:
- print(listing[0][0])
- #connect.set_option(ldap.OPT_REFERRALS, 0)
- try:
- #if authentication successful, get the full user data
- connect.simple_bind_s('cn={},ou={},ou=USERS,o=VSB'.format(username, trailing_context), password)
- result = connect.search_s(base_dn, ldap.SCOPE_SUBTREE, search_filter)
- # return all user data results
- connect.unbind_s()
- print(result)
- except ldap.LDAPError as e:
- print(e)
- connect.unbind_s()
- print('authentication error')
Add Comment
Please, Sign In to add comment