Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 :)

share|improve this question

2 Answers 2

up vote 0 down vote accepted

Instead of:

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

try:

if [[ -z $(dig @"$go" "$a" +short) ]]
share|improve this answer

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.