Skip to content

Instantly share code, notes, and snippets.

@tylerkerr
Created February 15, 2015 20:28
Show Gist options
  • Save tylerkerr/13e2c1097a5cc1090ef8 to your computer and use it in GitHub Desktop.
Save tylerkerr/13e2c1097a5cc1090ef8 to your computer and use it in GitHub Desktop.
tylerping. requires fping
function tp()
{
if [[ -z $1 ]]
then
echo "usage: tp [host]"
else
if [[ -z $2 ]]
then
timeout="1000"
else
timeout="$2"
fi
while :
do
res1=`fping -e -t $timeout -c 1 -A $1 2>&1`
result=`echo -n "$res1"`
checkexceed=`echo "$result" | grep -E "ICMP Time Exceeded"`
checkreply=`echo "$result" | grep -E "bytes"`
checkdns=`echo "$result" | grep -E "address not found"`
address=`echo "$result" | grep -Eo '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' | head -1`
if [[ "$1" = "$address" ]]
then
printaddress="$address"
else
printaddress="$1 ($address)"
fi
pingdate=`date +'%Y.%m.%d %H:%M:%S'`
if
[[ ! -z "$checkexceed" ]]
then
pingstatus="\033[0;35mexceeded ttl\033[0m"
printaddress="$1 (lh $address)"
elif [[ ! -z "$checkreply" ]]
then
pingtime=`echo "$result" | grep -Eo 'bytes.+\(' | awk '{print $2;}'`
pingms=`echo $pingtime | sed -E 's#\..*##'`
if [[ "$pingms" -lt 1 ]]
then
pingstatus="is \033[0;34mfast\033[0m - $pingtime ms"
elif [[ "$pingms" -lt 5 ]]
then
pingstatus="is \033[0;36mfast\033[0m - $pingtime ms"
elif [[ "$pingms" -lt 101 ]]
then
pingstatus="is \033[0;32mgood\033[0m - $pingtime ms"
else
pingstatus="is \033[0;33mslow\033[0m - $pingtime ms"
fi
elif [[ ! -z "$checkdns" ]]
then
pingstatus="\033[0;91mfailed\033[0m - DNS resolution unsuccessful"
else [[ -z "$checkreply" ]]
pingstatus="\033[0;31mtimed out\033[0m (timeout at ≥ $timeout ms)"
fi
if
[[ -z "$address" ]]
then
printaddress="$1"
fi
echo -e "$pingdate - $printaddress $pingstatus"
sleep 1
done
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment