-
-
Save dillera/652e60662d622ebbc585371f9c85f789 to your computer and use it in GitHub Desktop.
Export Users from Active Directory / LDAP to CSV file with Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# http://www.packtpub.com/article/python-ldap-applications-ldap-opearations | |
# sudo apt-get install python-ldap | |
import ldap | |
host = 'ldap://example.com:389' | |
dn = '[email protected]' | |
pw = 'secret' | |
base_dn = 'cn=users,dc=example,dc=com' | |
filter = 'memberOf=cn=workers,cn=users,dc=example,dc=com' | |
# Show only activated users | |
# filter = '(&(memberOf=cn=workers,cn=users,dc=example,dc=com)(!(userAccountControl=66050)))' | |
attrs = ['sAMAccountName', 'givenname', 'sn', 'mail', 'description', 'telephonenumber', 'homephone', 'mobile'] | |
con = ldap.initialize( host ) | |
# Bind to the server | |
con.simple_bind_s( dn, pw ) | |
res = con.search_s( base_dn, ldap.SCOPE_SUBTREE, filter, attrs ) | |
# Close the connection | |
con.unbind() | |
# Print the returned dictionary | |
print res | |
for i in res: | |
print i[1]['givenname'], i[1]['sn'] | |
# TODO: save as csv file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment