using ldapsearch to return only a value
Asked Answered
T

2

14

using an OPENLDAP server i want to retrieve informations from it with ldapsearch. I created a custom class called iduriclass, this class is used to store an id and an uri. in my ldapsearch command i want it to return only the uri for a specified id.

EXAMPLE : the directory contain now two entries id=test uri=server.com/test and id=test2 uri=server.com/test2

Trying it i get an ldif file that contains all uris in the server

I want to have an ldapsearch command that takes test as argument and returns only a value that is : server.com/test

Tooth answered 1/1, 2014 at 12:46 Comment(2)
Why not add your query string and the actual ldapsearch command used into your question?Kaohsiung
Can you show me how please :)Tooth
G
30

Here's how you query your ldap server.

HOSTNAME=<your ladap hostname>
USERNAME=<your ldap username>
PASSWORD=<your ldap username's password>
SEARCHBASE=<your ldap's search base DN>
QUERYSTRING=test1
PORT=<your ldap port>

ldapsearch -LLL -h ${HOSTNAME} -p $PORT -D cn=${USERNAME} -w ${PASSWORD} -b "${SEARCHBASE}" "(id=${QUERYSTRING})" uri | sed -n 's/^[ \t]*uri:[ \t]*\(.*\)/\1/p'

The option -LLL will not print ldap comments on output. Your ldap may require -x (simple authentication) if it doesn't support SASL.

Gewgaw answered 1/1, 2014 at 18:47 Comment(3)
i did this and the result was: dn: id=test,dc=example,dc=com uri: server.com/test/file i want it to output only : server.com/test/fileTooth
Check my edit. ldapsearch will always return the dn: so piping to sed and removing it and the attribute name uri: leaves only the attribute value.Gewgaw
The answer from badc0de spares you the parsing and is additionally working with multiple line attributes (as sshpupkeys might have)Pedant
S
5

Adding the parameter -tt writes a file with ONLY the requested attribute(s) value as the OP requested. No preceding field name or anything else. Path is configurable with -T, otherwise is /tmp

Shih answered 21/6, 2017 at 8:56 Comment(2)
-t[t] A single -t writes retrieved non-printable values to a set of temporary files. This is useful for dealing with values containing non-character data such as jpegPhoto or audio. A second -t writes all re‐trieved values to files.Dam
-T path Write temporary files to directory specified by path (default: /var/tmp/)Dam

© 2022 - 2024 — McMap. All rights reserved.