Condition to test if a host can be pinged

I have a much larger script that takes an input file of hosts and determines if we support them and checks to ensure the FQDN coincides with our DNS. For instance, the hostname may return a different FQDN when passed to the "host" command, so I keep the value of the output of the "host" command and also apply some conditions to determine if NXDOMAIN is returned.

In a stunning twist of fate I see that sometimes the file I'm reading returns the ip address of a machine with more than one NIC (and ip address), and usually that NIC is not in operation so it's screwing up my reporting. I need to put a condition in my script that not only checks to see if the ip returns a valid hostname using the "host" command, but now I also need to the script to ping the machine and ensure it is working.

The input file contains a hostname, and three fields following the hostname. Each field is reserved for an IP address - sometimes there's 1 ip, sometimes 2, or 3. I already have a test to determine if the ip address works:

# if tmp1 is not a zero value
        if [[ -n $tmp1 ]]
        then

# if hosting the value of tmp1 does not return NXDOMAIN (hence if it's a valid host)
# and if that host can be pinged
# then host that value and exit the loop
                if [[ -z `host $tmp1 | grep -o NXDOMAIN` ]]
                then
                        host $tmp1 | cut -d" " -f5 | sed 's/.$//g'
                else
                        if [[ -n $tmp2 ]]
                        then
                                if [[ -z `host $tmp2 | grep -o NXDOMAIN` ]]                                then
                                        host $tmp2 | cut -d" " -f5 | sed 's/.$//g'
                                else
                                        if [[ -z `host $tmp3 | grep -o NXDOMAIN` ]]
                                        then
                                                host $tmp3 | cut -d" " -f5 | sed 's/.$//g'
                                        fi
                                fi
                        fi
                fi
        else echo $i >> exceptions_$dt
        fi
done

This works fine as is. However now I need to add an additional condition that says "can the host be pinged also? If not exit the loop and move onto the next one". I tried to add this but it's somehow sending an invalid value to the host command:

if [[ -z `host $tmp1 | grep -o NXDOMAIN` && -n `ping -c 1 -w 1 $tmp1` ]]

I tried putting the ping condition in its own brackets separating the conditions by the ampersands but I'm still having a problem. Can anyone suggest how I can implement this additional check?

if ! host "$tmp1" | grep -qF NXDOMAIN && ping -c 1 -w 1 "$tmp1" > /dev/null 2>&1; then
  ...

P.S. I've noticed that you're using a GNU syntax (most likely you're executing the script on Linux),
so the -q option of grep should be available :slight_smile:

You don't have to run cut hundreds of times in backticks. Use the read command once.

while read FIELD1 FIELD2 FIELD3 FIELD4
do
        echo "Field 1 is "$FIELD1"
done < inputfile

'host' might be outputting to stderr, try 2>&1

Oh thank you so much - it works now!!!

I'm sure my script is very novice and sophmoric and I do plan to learn more but I'm just in a situation where, figuratively (not really), I've got a boss standing over me saying "YOU BETTER GET THOSE REPORTS IN ORDER AND YOU'RE NOT LEAVING THE OFFICE TILL YOU DO" so I'm just trying to do my best to maintain. I will check the suggestion you mentioned and read more on the bash scripting guide. Thank you both!

No insult intended of course, but the difference is important. It's very easy to make excruciatingly slow scripts by running external utilities thousands of times more than you need to instead of learning the proper way first. One of my first shell scripting experiments was a line-wrapper which wrapped lines slower than you could read them due to pipe and external utility abuse...

Also, I apologize for misunderstanding your original question. I thought you meant grep wasn't working, not that it was printing garbage.