First I want to apologize for being an absolute noob at programming, writing scripts, etc. I'm running a FreeNAS Server and a Raspberry Pi which I'd like to include a sh-Script which should check if 8 clients (IP addresses) are online. If yes they should stop but if all IP's are offline, it should perform another task / script.
Here's my problem: On the internet (src: https://www.tech-blogger.net/freenas-automatisch-bei-nicht-benutzung-abschalten/) I found a script which checks specific IP's and if they're offline it should shutdown automatically. When I ping those addresses I get 100% package lost which indicates that they are offline. But the script echos me that at least one client is offline. Because I'm not THAT dumb I assume that it has something to do with my code. Does anyone know what goes wrong with my Script?:
HOST1=192.168.1.32
HOST2=192.168.1.33
HOST3=192.168.1.34
HOST4=192.168.1.35
HOST5=192.168.1.36
_exit () {
case $1 in
1) echo „No Shutdown – At least one PC is online“ ;;
2) echo „No PC is online – Shutdown“ ; shutdown -p now ;;
esac
exit $1;
}
#Check if IPs are online
if [ `ping -c 1 -i 1 $HOST1 | grep -wc 100.0%` -eq 0 ] || [ `ping -c 1 -i 1 $HOST2 | grep -wc 100.0%` -eq 0 ] || [ `ping -c 1 -i 1 $HOST3 | grep -wc 100.0%` -eq 0 ] || [ `ping -c 1 -i 1 $HOST4 | grep -wc 100.0%` -eq 0 ] || [ `ping -c 1 -i 1 $HOST5 | grep -wc 100.0%` -eq 0 ] ; then _exit 1;
#All Clients are offline, shutdown
else
_exit 2
fi
fi
if [ $(cmd | grep -c ...) -eq 0 ]is an anti-pattern. Useif ! cmd | grep -q ...insteadgrepat all here.pingwill return 0 if it receives a response, non-zero otherwise, so you can just doif ( ping -c 1 -t 1 $HOST1 || ping -c 1 -t 1 $HOST2 || ... ) > /dev/null 2>&1 ; thento see if any host responds.