Last active
August 29, 2015 14:10
-
-
Save momota/3b4fce87bf4548201354 to your computer and use it in GitHub Desktop.
color ping result, and save log file
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 | |
# argument validation | |
if [ $# -eq 2 ];then | |
target=$1 | |
interval=$2 | |
log="`/bin/date +%Y%m%d%H%M%S`_`/bin/hostname -i`---$1_ping.log" | |
else | |
echo -e "USAGE: ./ping.sh <IP_ADDRESS or HOSTNAME> <PING_INTERVAL[sec]>\n" | |
exit 1 | |
fi | |
error_count=0 | |
is_alive_ping() | |
{ | |
/bin/ping -c 1 -i $2 -w 1 $1 > /dev/null | |
if [ $? -eq 0 ]; then | |
echo -e "\e[32mOK\e[m --- `date "+%b %d %R:%S.%N"` : Node with IP( $1 ) is up." | tee -a $3 | |
return 0 | |
else | |
echo -e "\e[31mNG\e[m --- `date "+%b %d %R:%S.%N"` : Node with IP( $1 ) is down." | tee -a $3 | |
return 1 | |
fi | |
} | |
trap_ctrl_c() | |
{ | |
echo -e "\n***** end ping *****\n" | |
echo -e "\nerror: $error_count" | tee -a $log | |
exit 0 | |
} | |
# ---------------------------------------------------------------------- | |
trap trap_ctrl_c SIGINT | |
i=1 | |
while true; | |
do | |
printf "$((i++))\t\t: " | |
is_alive_ping $target $interval $log | |
let error_count=$error_count+$? | |
sleep $interval | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment