I need help to update this script to if ping fails it would send a wakonlan call to another host (besides the email which is sent now if ping fails). How can this be done from this script?
This is what i like to achive:
Server1 is up > PingComputer pings server1 = everything is fine. Server 1 fails > PingComputer sends mail AND sends wakeonlan call to server 2 = server2 starts up.
Not so important (only if its possible): After a fail on server1 i would like to get a mail when server1 is up again and then this scenario starts over.
#!/bin/bash
HOSTS="IP ADRESS"
COUNT=4
for myHost in $HOSTS
do
count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | a$
if [ $count -eq 0 ]; then
# 100% failed
echo "Server failed at $(date)" | mail -s "Server Down" [email protected]
echo "Host : $myHost is down (ping failed) at $(date)"
fi
done
Im using ubuntu server on all 3 computers btw.
Specification
At least one host must be always pingable (ping success = UP).
When a pingable host becomes unreachable (ping failed) => mail and:
- if another host is already pingable => no problem => continue
- else => wakeonlan another host
When an unreachable host becomes pingable back => mail
When a host is still unreachable after wakeonlan it => mail
Script
The initial script was based on
bash
. But your Ubuntu Server seems to use abash
version too old without associative array. Fortunately,zsh
supports associative array for a while. Therefore the following script has been rewriten forzsh
. Moreover support for MAC ADDRESS has been also added.Script
one_host_should_ping.sh
:Usage
Give execution permission:
If you use two hosts:
If you use five hosts:
Example