Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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:
      ./ line 4: syntax error near unexpected token `?' ./ 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://963814]
Approved by Corion
Front-paged by Arunbear
[ambrus]: Corion: as for that, I talked with schmorp, and looked a bit more at Prima
[ambrus]: Corion: from the docs, it definitely looks like it would be possible to write an AnyEvent driver for it using Prima::File and Prima::Timer.
[ambrus]: (I haven't found an idle event.)
[ambrus]: However, I also looked at the internals, and found two interesting things:
[marto]: stupid xpath question, I have a xpath copied from the browser, is the * wildcard supposed to work for IDs? e.g. '//*[@id="*"]/ div/div/header/p/a '?
[ambrus]: The main loop for unixish systems is in https://metacpan. org/source/ KARASIK/Prima-1. 49/unix/apc_app.c. It is a hand-rolled select loop, but also always spins at least once every 0.2 seconds for some reason.
[ambrus]: marto: no, not that way. if you just want to test for an existing id attribute, then write *[@id] , or you can match a regexp to the value of that attribute like *[@id=~/something/ ]

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2016-12-08 10:48 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (140 votes). Check out past polls.