Skip to content

Instantly share code, notes, and snippets.

@AbirRazzak
Last active November 9, 2020 20:58
Show Gist options
  • Save AbirRazzak/fd1ef73e6e639a542c7012d22fd33a19 to your computer and use it in GitHub Desktop.
Save AbirRazzak/fd1ef73e6e639a542c7012d22fd33a19 to your computer and use it in GitHub Desktop.
Demo python-ldap script for senior design
import ldap
print('Connecting to ldap server...')
connect = ldap.initialize('ldap://localhost')
connect.set_option(ldap.OPT_REFERRALS, 0)
connect.simple_bind_s('cn=admin,dc=example,dc=com', 'redhat')
print('Connected!\n')
print('\nSearch for user with uid = amr439')
result = connect.search_s('dc=example,dc=com', ldap.SCOPE_SUBTREE, '(uid=amr439)')
for r in result:
print(r)
print('\nSearch for all user groups')
result = connect.search_s('dc=example,dc=com', ldap.SCOPE_SUBTREE, '(objectclass=groupOfNames)', ['cn'])
for r in result:
print(r)
print('\nSearch for all members in the "doctors" groups')
result = connect.search_s('dc=example,dc=com', ldap.SCOPE_SUBTREE, '(cn=doctors)', ['member'])
for r in result:
print(r)
@AbirRazzak
Copy link
Author

Installation:

sudo yum install python3
sudo yum install gcc
sudo yum install python3-devel
sudo yum install openldap-devel
sudo python3 -m pip install python-ldap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment