bash: exec 3<> /dev/tcp/$DEST/$port has a dns issue

bash exec does not resolve hostnames on my DNS server, but dig and nslookup work.

exec resolves names properly on other machines on the network not running the DNS.

Context: OSX 10.11.6; DNS & DHCP server running on the same machine (dnsmasq)


#!/bin/sh

DEST1="serial2.tool.irs."

DEST2=`dig +short $DEST1`

if exec 3<> /dev/tcp/$DEST1/10001; then

echo "OK $DEST1 "

exec 3>&-

else

echo "BAD $DEST1 "

fi

if exec 3<> /dev/tcp/$DEST2/10001; then

echo "OK $DEST2 "

exec 3>&-

else

echo "BAD $DEST2 "

fi

exit


OUTPUT:

/datalog/status/dnstest.sh: line 4: serial2.tool.irs.: nodename nor servname provided, or not known

/datalog/status/dnstest.sh: line 4: /dev/tcp/serial2.tool.irs./10001: Invalid argument

BAD serial2.tool.irs.

OK 10.0.5.182

bash-3.2#

Replies

Looks like the stumbling block for exec is the string "serial" at the beginning of the hostname:

xserial, ser-4, ser-four, ser4, serfour all resolve fine

serialfour, serial-four, serial4 only resolve with nslookup or dig

Does

ping
resolve these names?

The reason I ask is that

dig
and
nslookup
don’t go through the system resolver. Rather, they manually talk to one specific DNS server (that server defaults to the system’s default DNS server but the system’s DNS is way to complex to be represented by a single DNS server).

OTOH, I suspect that

bash
is going through the system resolver. I know that
ping
does, so if
ping
and
bash
behave the same then we know what’s causing this discrepancy.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

- when I tested it today, it worked properly; so there is some other variable involved that may clear up with time.

- i rebooted the machine and the problem returned, and both exec and the ping (as requested) fail for the hostname "serial4.tool.irs", but "e-serial4.tool.irs" is happy.

- the following transcript ofthe failures is from after the reboot (I also tested the hostname "e-serial.tool.irs" in the script and it works properly, then re-tested "serial4.tool.irs" and it still failed):

**********

Alpha:status eowyn$ ping e-serial4.tool.irs

PING e-serial4.tool.irs (10.0.5.184): 56 data bytes

64 bytes from 10.0.5.184: icmp_seq=0 ttl=64 time=0.346 ms

64 bytes from 10.0.5.184: icmp_seq=1 ttl=64 time=0.381 ms

64 bytes from 10.0.5.184: icmp_seq=2 ttl=64 time=0.352 ms

64 bytes from 10.0.5.184: icmp_seq=3 ttl=64 time=0.564 ms

^C

--- e-serial4.tool.irs ping statistics ---

4 packets transmitted, 4 packets received, 0.0% packet loss

round-trip min/avg/max/stddev = 0.346/0.411/0.564/0.089 ms

Alpha:status eowyn$ ping serial4.tool.irs

ping: cannot resolve serial4.tool.irs: Unknown host

Alpha:status eowyn$

Alpha:status eowyn$ dig +short e-serial4.tool.irs

10.0.5.184

Alpha:status eowyn$ dig +short serial4.tool.irs

10.0.5.184

Alpha:status eowyn$ ping serial4.tool.irs

ping: cannot resolve serial4.tool.irs: Unknown host

Alpha:status eowyn$ ping serial4.tool.irs.

ping: cannot resolve serial4.tool.irs.: Unknown host

Alpha:status eowyn$ ./dnstest.sh

10.0.5.184 = serial4.tool.irs.

OK 10.0.5.184

./dnstest.sh: line 11: serial4.tool.irs.: nodename nor servname provided, or not known

./dnstest.sh: line 11: /dev/tcp/serial4.tool.irs./10001: Invalid argument

BAD serial4.tool.irs.

****** dnstest.sh ******

#!/bin/sh

DEST2="serial4.tool.irs."

DEST1=`dig +short "$DEST2"`

echo "$DEST1 = $DEST2 "

if exec 3<> "/dev/tcp/$DEST1/10001"; then

echo "OK $DEST1 "

exec 3>&-

else

echo "BAD $DEST1 "

fi

if exec 3<> "/dev/tcp/$DEST2/10001"; then

echo "OK $DEST2 "

exec 3>&-

else

echo "BAD $DEST2 "

fi

exit