LDAP search in script results in "DNS SRV: out of memory"

Viewed 302

I am trying to write a script that will declare txt file & a specified server, run an LDAP query, and put the output in the txt file, but I am getting an error DNS SRV: out of memory? It is a CentOS 7 VM and has 8GB RAM.

#!/bin/bash -x

# Declare vars
outFile="file/path"
dc="my.dc.com"

# Run openldap query
hosts=$(ldapsearch -b "ou=my,dc=ou,dc=path" -H ldap://{$dc} | grep dNSHostName)

# echo to file to test
echo {$hosts} >&2 {$outFile}
exit 0

This is the output I get:

+ outFile=file/path
+ dc=my.dc.com
++ ldapsearch -b "ou=my,dc=ou,dc=path" -H 'ldap://{my.dc.com}'
++ grep dNSHostName
DNS SRV: out of memory?
+ hosts=
+ echo '{}' '{file/path}'
{} {file/path}
+ exit 0

However, when I run just the LDAP query outside of the script, it returns successfully.

The dc has 16GB of RAM. I monitored the RAM during the query and no visible spikes were observed.

EDIT:

[prompt]$ ldapsearch -b "ou=my,dc=ou,dc=path" -H ldap://my.dc.com | grep dNSHostName
SASL/GSSAPI authentication started
SASL username: user@domain.com
SASL SSF: 256
SASL data security layer installed.
dNSHostName: hostname1.com
dNSHostName: hostname2.com
dNSHostName: hostname3.com
dNSHostName: hostname4.com
dNSHostName: hostname5.com
dNSHostName: hostname6.com
dNSHostName: hostname7.com
1 Answers

There is a bug opened on bugzilla.redhat.com that refers to your issue, but it's not going to be fixed. The error message (DNS SRV: out of memory?) is probably misleading and it might just be that the LDAP client failed to resolve the server name.

The server URL in your script is ldap://{$dc} but your command line (which works), doesn't include the curly brackets. Try this:

 hosts=$(ldapsearch -b "ou=my,dc=ou,dc=path" -H "ldap://$dc" | grep dNSHostName)

It seems to me you don't know how to use a variable in Bourne shell. The proper syntax is "${variable}" (or just "$variable" if there is no ambiguity). The double quotes are not strictly necessary but I strongly advise you to always expand variables inside double quotes, unless you must not (rare cases that you will recognize when they will occur).

Thus, also replace this:

echo {$hosts} >&2 {$outFile}

with this:

echo "$hosts" >&2 "$outFile"

or this:

echo "${hosts}" >&2 "${outFile}"
Related