Search code examples
bashvirtualboxcentos7openldap

Automating ldapsearch with bash using variables


I perform an ldapsearch like

ldapsearch -D "cn=ldapadm,dc=test,dc=com" -w ldappasswd -b "dc=test,dc=com" objectclass=*

and it works fine.

Now I want to substitute the input with varaibles:

BASEDN="cn=ldapadm,dc=test,dc=com"
PASSWD="ldappasswd"
BINDDN="dc=test,dc=com"

ldapsearch -D "${BASEDN}" -w "${PASSWD}" -b "${BINDDN}" objectclass=*

But I get an

ldap_bind: Invalid credentials (49)

error... The problematic part here is the PASSWD substitution. The other two substitutions are working fine.

I am using CentOS7 on VirtualBox. The password does not contain special characters.

What am I missing here?


Solution

  • set -x; ldapsearch -D "${BASEDN}" -w "${PASSWD}" -b "${BINDDN}" objectclass=*
    

    revealed the problem. I loaded the values from a file and there was an invisible linebreak at the end of each string.

    Output:

    ldapsearch -D cn=ldapadm,dc=test,dc=com\r -w ldappasswd\r -b dc=test,dc=com\r objectclass=*
    

    I removed \r calling

    PASSWD=`echo "${value_from_file}"| sed 's/\\r//g'`
    

    and it worked.