Skip to content

Instantly share code, notes, and snippets.

@lidopaglia
Created August 7, 2021 15:47
Show Gist options
  • Save lidopaglia/d961b12693ff3690612630cac4dbd0b0 to your computer and use it in GitHub Desktop.
Save lidopaglia/d961b12693ff3690612630cac4dbd0b0 to your computer and use it in GitHub Desktop.
python-ldaps
#!/usr/bin/python2
# -*- coding: utf-8 -*-
import sys
import getpass
import ldap
def getLdapUsers():
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
l = ldap.initialize('ldaps://ldap.example.com')
try:
password = getpass.getpass("Please enter the ldap admin password: ")
l.simple_bind_s("cn=admin,dc=example,dc=com", password)
baseDN = "ou=people,o=example,dc=example,dc=com"
searchScope = ldap.SCOPE_SUBTREE
searchFilter = "cn=*"
users = {}
ldap_result_id = l.search(baseDN, searchScope, searchFilter)
while 1:
rType, rData = l.result(ldap_result_id, 0)
if (rData == []):
break
else:
if rType == ldap.RES_SEARCH_ENTRY:
cn = rData[0][0]
data = rData[0][1]
#Flatten, just for more easy access
for (k, v) in data.items():
if len(v) == 1:
data[k] = v[0]
uid = data["uid"]
users[cn] = data
return users;
except ldap.LDAPError, e:
print e
finally:
l.unbind_s()
return 0
def main():
ldapUsers = getLdapUsers()
print(ldapUsers)
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment