Last active
October 11, 2015 15:27
-
-
Save tatic0/3879498 to your computer and use it in GitHub Desktop.
as soon as the server replies to ping, do something
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ifping.sh | |
# f varas 10 2012 | |
## | |
## you need to have sshpass or ssh-key | |
## to grant "passwordless" access to the servers | |
# execute an action when remote server replies to ping | |
counter=1 | |
USAGE="usage: $0 hostname {uptime|uname|fallback|script}" | |
if [ -z $1 ]; then | |
echo "$USAGE" | |
exit 1 | |
else | |
while true; do | |
ping -c1 $1 > /dev/null | |
if [ $? == 0 ]; then | |
echo "doing $2" | |
if [ -z $2 ]; then | |
echo "please, be more specific" | |
exit 2 | |
else | |
case $2 in | |
uptime) | |
ssh $1 'uptime' | |
;; | |
uname) | |
ssh $1 'uname -a' | |
;; | |
fallback) | |
ssh $1 echo "\`timeout -s SIGKILL 10 grep -i fall /var/log/syslog | tail \`" | |
;; | |
script) | |
./script.sh $1 | |
;; | |
*) | |
echo "$USAGE" | |
esac | |
fi | |
exit 0 | |
else | |
echo "$1 unreachable, tried $counter times" | |
fi | |
sleep 5 | |
let counter++ | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment