I have a script that ask if the ping of a certain array of macs is online.
#!/bin/bash
#Array of Mac hostnames separated by spaces
my_macs=( Mac111 Mac121 Mac122 Mac123 Mac124 Mac125 Mac126 Mac127 Mac128 Mac129 )
# Number of days the remote Mac is allowed to be up
MAX_UPDAYS=7
CURR_TIME=$(date +%s)
MAX_UPTIME=$(( MAX_UPDAYS * 86400 ))
ADMINUSER="admusr"
#Steps through each hostname and issues SSH command to that host
#Loops through the elements of the Array
echo "Remote shutdown check started at $(date)"
for MAC in "${my_macs[@]}"
do
echo -n "Checking ${MAC}... "
# -q quiet
# -c nb of pings to perform
if ping -q -c3 "${MAC}" >/dev/null; then
echo "is up. Getting boot time... "
BOOT_TIME=0
# Get time of boot from remote Mac
BOOT_TIME=$(ssh "${ADMINUSER}@${MAC}" sysctl -n kern.boottime | sed -e 's/.* sec = \([0-9]*\).*/\1/')
if [ "$BOOT_TIME" -gt 0 ] && [ $(( CURR_TIME - BOOT_TIME )) -ge $MAX_UPTIME ]; then
echo "${MAC} uptime is beyond MAX_UPDAYS limit. Sending shutdown command"
ssh "${ADMINUSER}@${MAC}" 'sudo /sbin/shutdown -h now'
else
echo "${MAC} uptime is below limit. Skipping shutdown."
fi
else
echo "is down (ping failed)"
fi
done
The problem is that the script stops if it can't resolve the hostname of one of those machines (yes this can happen quite often, I don't want to go into details why) The hostnames are definitely right, so I want to tell the script to first search for the hostname and if he can be resolved it will resume. Otherwise it will cancel it for this one mac. Is this possible?
exit
orset -e
or anything in this code that make this script abort if a name resolution fails. What I see in that case is a spurious "Mac is down" and the loop then continues with the next Mac. So my question is: where and why does this script stop if it can't resolve the hostname?