http://www.perlmonks.org?node_id=963814

*update* : simplification with xx_XX.UTF-8 "locale" (see below)

When you are using ldapsearch to find quickly some informations in LDAP directory, there are some problems.

  1. Some lines are wrapped (several lines for a single attribute-value).
  2. Some lines are encoded in base64 (those with utf8 content).

For instance, with this command :

ldapsearch -x -LLL -h myldaphost -b 'ou=myldapbase,...'

Just "pipe" the result in this Perl filter :

ldapsearch ... | perl -MMIME::Base64 -MEncode=decode -n -00 -e 's/\n +//g;s/(?<=:: )(\S+)/decode("UTF-8",decode_base64($1))/eg;print' [or] ldapsearch ... | perl -MMIME::Base64 -MEncode=decode -n -00 -e 's/\n +//g;s/(?<=:: )(\S+)/decode("UTF-8",decode_base64($1))/eg;print if /Be +yoncé/'

This way, the Lord of the Rings can see the entire block (LDAP entry) matched by the regex you want (a single lady, in this example).

Warning : the output is NOT LDIF-compatible, it's just 'text'.


update : depending of your context (locale with or without UTF-8), you can simplify the command-line, like this:

ldapsearch ... | perl -MMIME::Base64 -n -00 -e 's/\n //g;s/(?<=:: )(\ +S+)/decode_base64($1)/eg;print' [or] ldapsearch ... | perl -MMIME::Base64 -n -00 -e 's/\n //g;s/(?<=:: )(\ +S+)/decode_base64($1)/eg;print if /Beyoncé/'

Replies are listed 'Best First'.
Re: Readable ldapsearch output
by i5513 (Pilgrim) on Apr 06, 2012 at 17:09 UTC

    There was a patch propposed to add --ldif-wrap (and solve your first point, but it was not accepted (seems like it is really not a bug)).

    Too, there is a bug (archived), where there is a request to add a similar perl script to documentation.

Re: Readable ldapsearch output
by Anonymous Monk on Jul 25, 2013 at 00:46 UTC
    Thank you for this
Re: Readable ldapsearch output
by Anonymous Monk on Sep 09, 2013 at 19:25 UTC
    THANK YOU!!! It worked immediately, no hacking required.
Re: Readable ldapsearch output
by Anonymous Monk on Sep 19, 2014 at 08:14 UTC
    THANK YOU ! I added your magic to my .bashrc: alias ldapdecode="perl -MMIME::Base64 -n -00 -e 's/\n //g;s/(?<=:: )(\S+)/decode_base64(\$1)/eg;print'" and just do ldapsearch | ldapdecode Again, Thank you A LOT !!
      I'm getting the following error:
      ./getDN.sh: line 4: syntax error near unexpected token `?' ./getDN.sh: line 4: `ldapsearch -h platform-vip -p 7323 -D "administra +torName=jambala, nodeName=jambala" -w -b "$1" "(objectClass=*)" -x - +P 3 -s sub | perl -MMIME::Base64 -n -00 -e 's/\n //g;s/(?<=:: )(\S+) +/decode_base64($1)/eg;print''
      here is my perl version:
      This is perl, v5.10.0 built for x86_64-linux-thread-multi

      Any advice?

        Any advice?

        Forget about oneliners, write programs, much easier to read and debug

Re: Readable ldapsearch output
by Anonymous Monk on Jul 29, 2015 at 12:58 UTC
    Thanks. it also works with ldapvi!
      Thanks guys. I used the -T option in ldapsearch to work around the LDIF line wrapping issue.
        To avoid wrapping, add: -o ldif-wrap=no