Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Parse an ldif file extracting the userPassword field
#!/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