Created
April 10, 2019 23:09
-
-
Save thesubtlety/ccfa1563120c8f8dc48c51d6b047cbe5 to your computer and use it in GitHub Desktop.
Parse an ldif file extracting the userPassword field
This file contains 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/env python | |
# pip install python-ldap | |
import sys, ldif | |
def main(): | |
if len(sys.argv) != 3: | |
print("Usage: %s <ldif_dump.ldif> <outfile>") % sys.argv[0] | |
sys.exit(-1) | |
ldif_file = sys.argv[1] | |
outf = sys.argv[2] | |
out = [] | |
with open(ldif_file, 'r') as ldiff: | |
parser = ldif.LDIFRecordList(ldiff) | |
parser.parse() | |
for user in parser.all_records: | |
out.append({k: user[1].get(k,None) for k in ['userPassword', 'cn']}) | |
with open(outf,'w') as outfile: | |
for each in out: | |
try: | |
outfile.write(each['cn'][0] + ":" + each['userPassword'][0] + "\n") | |
except: | |
pass | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment