Last active
August 29, 2015 14:08
-
-
Save richardhsu/ec70e54b647ea89b9f67 to your computer and use it in GitHub Desktop.
Ping with timestamp and threshold.
This file contains hidden or 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 | |
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