Skip to content

Instantly share code, notes, and snippets.

@Forceflow
Last active February 11, 2022 13:34
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 Forceflow/dd67cd69b85759c18d0bebb1b6dde707 to your computer and use it in GitHub Desktop.
Save Forceflow/dd67cd69b85759c18d0bebb1b6dde707 to your computer and use it in GitHub Desktop.
Packet Loss Test
#!/bin/bash
# This tool repeatedly sends 100 packets to an internet server (standard: 1.1.1.1 - Cloudflare can take it)
# in the shortest interval ping allows (0.2s) and reports graphically how many packets were lost.
#
# Requirements: bash, ping, (g)awk
TARGET=1.1.1.1
SCRIPTNAME="loss_test"
# Nice log function
getDATE () { date '+%F %T'; }
NCB='\033[0m'; BLUEBG='\033[44m\033[1m'; GREENBG='\033[42m\033[1m'; REDBG='\033[41m\033[1m'; YELLOWBG='\033[43m\033[1m'
logNice() { if [ "$1" == "ok" ]; then echo -e "${GREENBG}->${NCB} $(getDATE) ($SCRIPTNAME): $2"
elif [ "$1" == "warn" ]; then echo -e "${YELLOWBG}->${NCB} $(getDATE) ($SCRIPTNAME): $2"
elif [ "$1" == "fail" ]; then echo -e "${REDBG}x>${NCB} $(getDATE) ($SCRIPTNAME): $2" >&2
elif [ "$1" = "start" ] || [ "$1" = "stop" ]; then echo -e "${BLUEBG}-> $(getDATE) ($SCRIPTNAME): $2${NCB}"
else echo -e "${BLUEBG}->${NCB} $(getDATE) ($SCRIPTNAME): $2"; fi }
# Create empty string of length $1 to draw packets
createString(){
MAX=$(echo "$1" | awk '{print ($0-int($0)>0)?int($0)+1:int($0)}')
for ((i=1;i<="$MAX";i++))
do
echo -n " "
done
}
logNice start "Starting packet loss monitoring"
while true
do
LOSS_DEC=$(ping -i 0.2 -c 100 -q $TARGET | awk '/packet loss/ { print strtonum($6) }')
LOSS=$(echo "$LOSS_DEC" | awk '{print ($0-int($0)>0)?int($0)+1:int($0)}')
if [ "$LOSS" -eq "0" ]; then
logNice ok "$LOSS \t ${GREENBG} ${NCB}"
else
LOSS_STRING=$(createString "$LOSS")
logNice fail "$LOSS \t ${REDBG}${LOSS_STRING}${NCB}"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment