1

I'm using following script to dig a domain on multiple servers. But if a domain does not exist then also the output is "Resolved by server: domain.com ;". It just because the execution of the command returns 0 . Can any one help me if there are not records this should inform correct statement instead of printing wrong statement.

#!/bin/bash

a=$1
go="8.8.8.8"
f2="4.2.2.2"


if [ "$a" = "" ]
        then
echo "Usage: `basename $0` domain.com"
exit 1
        else

dig @"$go" "$a" +short
if  [ "$?" != 0 ]
        then
        echo "Server: $go did not resolve: $a ; "
        else
        echo "Resolved by $go: $a ; "
        echo " "
fi
        fi

if [[ -z $(dig @"$go" "$a" +short) ]] worked for me thanks :)

2 Answers 2

0

Instead of:

dig @"$go" "$a" +short
if  [ "$?" != 0 ]

try:

if [[ -z $(dig @"$go" "$a" +short) ]]
1

dig is a tool to check for problems on a DNS server, since a NXDOMAIN is not a problem it will return 0.
dig will throw other codes e.g. when the server is down, the query is refused...

Switch to a different tool like host:

$ host serverfault.com 8.8.8.8 >/dev/null
$ echo $?
0
$ host serverfaultxyz.com 8.8.8.8 >/dev/null
$ echo $?
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .