Skip to content

Instantly share code, notes, and snippets.

@Forceflow
Last active February 11, 2022 13:34
Show Gist options
  • Select an option

  • Save Forceflow/dd67cd69b85759c18d0bebb1b6dde707 to your computer and use it in GitHub Desktop.

Select an option

Save Forceflow/dd67cd69b85759c18d0bebb1b6dde707 to your computer and use it in GitHub Desktop.

Revisions

  1. Forceflow revised this gist Feb 11, 2022. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion loss_test.sh
    Original file line number Diff line number Diff line change
    @@ -6,10 +6,11 @@
    # Requirements: bash, ping, (g)awk

    TARGET=1.1.1.1
    SCRIPTNAME="loss_test"

    # Nice log function
    getDATE () { date '+%F %T'; }
    SCRIPTNAME="loss_test"; NCB='\033[0m'; BLUEBG='\033[44m\033[1m'; GREENBG='\033[42m\033[1m'; REDBG='\033[41m\033[1m'; YELLOWBG='\033[43m\033[1m'
    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
  2. Forceflow revised this gist Feb 11, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions loss_test.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    #!/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.
    # 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

  3. Forceflow created this gist Feb 11, 2022.
    39 changes: 39 additions & 0 deletions loss_test.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    #!/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

    # Nice log function
    getDATE () { date '+%F %T'; }
    SCRIPTNAME="loss_test"; 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