Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://963814]
Approved by Corion
Front-paged by Arunbear
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2017-05-27 16:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?