LDAPSEARCH into table format

10,968

Solution 1

Just in case someone else has to do this:

Based on the answer provided in Filter ldapsearch with awk/bash

this will output the LDAP info into a csv format:

$ ldapsearch -x -D "cn=something" | awk -v OFS=',' '{split($0,a,": ")} /^mail:/{mail=a[2]} /^uidNumber:/{uidNumber=a[2]} /^uid:/{uid=a[2]} /^cn/{cn=a[2]; print uid, uidNumber,cn , mail}' > ldap_dump.csv

NOTE You need to be careful about the order in which you parse the LDAP data with awk! It needs to be parsed in the same order as it appears on the LDAP data!

Solution 2

You can use the excellent miller tool (mlr)

The last bit:

echo output | sed 's/://g'  | mlr --x2c cat then unsparsify

How it works:

  • the sed converts the output to XTAB format
  • --x2c converts XTAB to CSV
  • cat then unsparsify makes sure the missing values are just filled instead of breaking to different csv output

Total command:

ldapsearch -H ldap://<hostname>:389 -D "<bindDN>" -W -b "<base>" '<query>' -oldif-wrap=no -LLL cn mail telephoneNumber | sed 's/://g'  | mlr --x2c cat then unsparsify
Share:
10,968
jorgehumberto
Author by

jorgehumberto

Updated on June 05, 2022

Comments

  • jorgehumberto
    jorgehumberto almost 2 years

    Is there any way to perform a LDAP search and save the results into a table format (e.g. csv)?

    Cheers Jorge

    • jwilleke
      jwilleke about 6 years
      Perform a LDAP search from with what LDAP Client?
    • jorgehumberto
      jorgehumberto about 6 years
      @jwilleke: openldap-2.3.43
  • Sebastian Stark
    Sebastian Stark over 2 years
    How would you deal with base64 encoded values in this pipeline?