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?
