Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Readable ldapsearch output

by brx (Pilgrim)
on Apr 06, 2012 at 11:21 UTC ( #963814=CUFP: print w/ replies, xml ) Need Help??

*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é/'

Comment on Readable ldapsearch output
Select or Download Code
Re: Readable ldapsearch output
by i5513 (Monk) 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 !!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://963814]
Approved by Corion
Front-paged by Arunbear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2015-06-02 06:15 GMT
Find Nodes?
    Voting Booth?

    What kind of chocolate gives you the most pleasure?

    Results (72 votes), past polls