Skip to content

Instantly share code, notes, and snippets.

@richardhsu

richardhsu/ping.sh

Last active Aug 29, 2015
Embed
What would you like to do?
Ping with timestamp and threshold.
#!/bin/bash
COUNT=450
INTERVAL=2
THRESHOLD=5
print_usage () {
echo "Usage: ./ping.sh [-h] [-c count] [-i interval] [-t threshold] -d destination"
echo " -h|--help : Print usage."
echo " -c|--count : Number of pings to send. Default 450."
echo " -i|--interval : The interval in seconds between each ping. Default 2."
echo " -t|--threshold : The threshold to show ping results in milliseconds. Default 5."
echo " -d|--destination : The destination host."
}
while [[ $# -ge 1 ]]
do
key="$1"
shift
# Case through the flags
case $key in
-h|--help)
print_usage
exit 1
;;
-i|--interval)
INTERVAL=$1
shift
;;
-c|--count)
COUNT=$1
shift
;;
-t|--threshold)
THRESHOLD=$1
shift
;;
-d|--destination)
DESTINATION=$1
shift
;;
*)
;;
esac
done
if [[ -z "$DESTINATION" ]]
then
echo "ERROR: You need a destination to ping using the -d flag."
print_usage
exit 1
fi
ping -i $INTERVAL -c $COUNT $DESTINATION | awk '$8 ~/^time=/ { gsub(/time=/, ""); if ($8 > $THRESHOLD) print strftime("%Y-%m-%d %H:%M:%S")"\t"$0; }'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment