Skip to content

Instantly share code, notes, and snippets.

@redperadot
Last active August 29, 2015 13:55
Show Gist options
  • Save redperadot/8715208 to your computer and use it in GitHub Desktop.
Save redperadot/8715208 to your computer and use it in GitHub Desktop.
Do you ping constantly but only want to know when a host is down? Well this little script does just that.
#!/usr/bin/env bash
# [email protected]
# host_down.sh
[[ $1 == "-a" ]] && audible='\a' && shift || audible=
address_1=$1
address_2=$2
[[ -z $address_1 ]] && read -p "Address #1 to ping: " address_1
[[ -z $address_2 ]] && read -p "Address #2 to ping: " address_2
localip="$(ifconfig | grep inet | grep -v inet6 | awk '{ print $2 }' | grep -v '^127' | tr '\n' ',' | sed 's/,$//')"
print_length="${address_1}${address_2}"
print_length="${#print_length}"
header_boarder="$(printf '%*s\n' "$(($print_length + 30))" '' | tr ' ' -)"
[[ $((${#header_boarder} % 2)) == 0 ]] && header_boarder="${header_boarder}-"
printf "\e[8;$((${#header_boarder} / 2 + $((${#header_boarder} / 4))));$(( ${#header_boarder} + 4 ))t"
sleep .2
COLUMNS=$(tput cols)
clear
echo "Pinging from $(hostname) ($localip)" | fmt -c -w $COLUMNS
echo "Address #1: $address_1 | Address #2: $address_2" | fmt -c -w $COLUMNS
echo "$header_boarder" | fmt -c -w $COLUMNS
while true; do
if [[ $day_stamp != "$(date "+%A %b %d %Y")" ]]; then
day_stamp="$(date "+%A %b %d %Y")"
echo "$day_stamp" | fmt -c -w $COLUMNS
fi
if [[ $(ping -t 2 -c 1 $address_1 > /dev/null 2>&1 ; echo $?) == "0" ]]; then
address_1_down=false
else
address_1_down=true
fi
if [[ $(ping -t 2 -c 1 $address_2 > /dev/null 2>&1 ; echo $?) == "0" ]]; then
address_2_down=false
else
address_2_down=true
fi
time_stamp="$(date "+%r")"
[[ $address_1_down == true ]] && notice=" drop @ $time_stamp" || notice=" "
[[ $address_1_down == true || $address_2_down == true ]] && notice="${notice} | "
[[ $address_2_down == true ]] && notice="${notice}drop @ $time_stamp" || notice="${notice} "
[[ $address_1_down == true || $address_2_down == true ]] && printf "%*s$audible\n" $(((${#notice}+$COLUMNS)/2)) "$notice"
sleep 1
done
@redperadot
Copy link
Author

To quickly run, use:

bash <(curl -fsSL https://gist.github.com/redperadot/8715208/raw/host_down.sh)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment