Skip to content

Instantly share code, notes, and snippets.

@MrMino
Created July 12, 2016 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrMino/44151f732e9355e42c98aa0326f8927f to your computer and use it in GitHub Desktop.
Save MrMino/44151f732e9355e42c98aa0326f8927f to your computer and use it in GitHub Desktop.
#!/bin/bash
function usage
{
echo "Usage: $0 <-a addr> [-n iterations] [-nn count]"
exit 1
}
# If not enough arguments show usage
if [[ $# -lt 1 ]]
then
usage
fi
# Default args
iter=1
count=1
# Argument parsing
while getopts ":a:i:c:" o; do
case "${o}" in
a)
addr=${OPTARG}
;;
i)
iter=${OPTARG}
;;
c)
count=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
# If addr wasn't specified show usage
if [ -z "$addr" ]; then usage; fi
# Sums of each statistic
sum_pkt=0
sum_rcv=0
sum_loss=0
sum_time=0
sum_avg=0
sum_mdev=0
rtt_min=0
rtt_max=0
# Loop through pings
for (( i=1; i<=iter; i++ ))
do
echo "iteration $i ==========================="
output=$(ping $addr -c $count | tail -n 2)
echo $output
# If ping failed don't do anything else
if (($? > 0)); then
continue
fi
echo "[ ?? ] No break"
# Exclude %, exclude 'ms', replace slashes with spaces give out only 1, 4, 6, 10 and 14'th column
parsed=$(echo $output | tr '\n' ' ' | sed 's/%//g; s/ms//g; s/\// /g'| cut -d' ' -f1,4,6,10,17,18,19,20)
# Split string to array
arr=(`echo ${parsed}`);
echo $parsed
# Recording data for stats
sum_pkt=$(echo "$sum_pkt+${arr[0]}" | bc)
sum_rcv=$(echo "$sum_rcv+${arr[1]}" | bc)
sum_loss=$(echo "$sum_loss+${arr[2]}" | bc)
sum_time=$(echo "$sum_time+${arr[3]}" | bc)
if [[ $(bc <<< "$rtt_min>${arr[4]}") -eq "1" ]]; then rtt_min=arr[4]; fi
if [[ $(bc <<< "$rtt_max<${arr[6]}") -eq "1" ]]; then rtt_max=arr[6]; fi
sum_avg=$(echo "scale=3; $sum_avg+${arr[5]}" | bc)
sum_mdev=$(echo "scale=3; $sum_mdev+${arr[7]}" | bc)
done
# Averaging the average
sum_avg=$(echo "scale=3; $sum_avg/$iter" | bc)
sum_mdev=$(echo "scale=3; $sum_mdev/$iter" | bc)
echo
echo "Overall statistic"
echo "$sum_pkt packets transmitted, $sum_rcv received, $sum_loss% packet loss, time $sum_time ms"
echo "rtt (iterations) min/avg/max/mdev = $sum_min/$sum_avg/$sum_max/$sum_mdev ms"
# sum_pkt packets transmitted, sum_rcv received, sum_loss packet loss, time sum_time rtt min/avg/max/mdev = sum_min/sum_avg/sum_max/sum_mdev ms
# 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.033/0.033/0.033/0.000 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment