Accessing LDAP with Program
To Access LDAP with a Programm that you can execute we wrote this Python file that takes a uid and gives Back the ldap entry for the uid.
For the Programm to run you need to first install the ldap3 python library
pip3 install ldap3
Then you can add this Python file called ldap.py
import sys
from ldap3 import Server, Connection, SUBTREE, ALL_ATTRIBUTES
# Set your LDAP server information
ldap_server = 'sdi01a.mi.hdm-stuttgart.de'
base_dn = 'dc=betrayer,dc=com'
def main():
if len(sys.argv) != 2:
print("Usage: python ldap.py <uid>")
sys.exit(1)
uid = sys.argv[1]
server = Server(ldap_server, port=389, get_info=ALL_ATTRIBUTES)
conn = Connection(server, user=f'cn=admin,{base_dn}', password='admin', auto_bind=True)
search_filter = f'(uid={uid})'
attributes = ['cn', 'uid', 'mail'] # Add more attributes as needed
conn.search(search_base=base_dn, search_filter=search_filter, search_scope=SUBTREE, attributes=attributes)
if conn.entries:
entry = conn.entries[0]
print("LDAP Entry Attributes:")
print("-------")
for attribute in entry:
print(attribute)
else:
print("No LDAP entry found for the given uid.")
conn.unbind()
if __name__ == "__main__":
main()
Execute Programm
When you execute the Programm in the command line like this
python ldap.py mehl
You should get this output
LDAP Entry Attributes:
-------
[email protected]
Benjamin Mehl
mehl