Skip to content

Instantly share code, notes, and snippets.

@thesubtlety
Created April 10, 2019 23:09
Show Gist options
  • Save thesubtlety/ccfa1563120c8f8dc48c51d6b047cbe5 to your computer and use it in GitHub Desktop.
Save thesubtlety/ccfa1563120c8f8dc48c51d6b047cbe5 to your computer and use it in GitHub Desktop.
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