Skip to content

Instantly share code, notes, and snippets.

@vikrum
Last active September 1, 2023 17:00
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 vikrum/f89c1c3c1cf3030846ddce433d9a2122 to your computer and use it in GitHub Desktop.
Save vikrum/f89c1c3c1cf3030846ddce433d9a2122 to your computer and use it in GitHub Desktop.
ping.11.sh
#!/bin/bash
# <xbar.title>ping</xbar.title>
# <xbar.version>v1.1</xbar.version>
# <xbar.author>Trung Đinh Quang, Grant Sherrick and Kent Karlsson</xbar.author>
# <xbar.author.github>thealmightygrant</xbar.author.github>
# <xbar.desc>Sends pings to a range of sites to determine network latency</xbar.desc>
# <xbar.image>http://i.imgur.com/lk3iGat.png?1</xbar.image>
# <xbar.dependencies>ping</xbar.dependencies>
# This is a plugin of Bitbar
# https://github.com/matryer/bitbar
# It shows current ping to some servers at the top Menubar
# This helps me to know my current connection speed
# vsn: sans color fork
#
# Authors: (Trung Đinh Quang) trungdq88@gmail.com and (Grant Sherrick) https://github.com/thealmightygrant
# Configuration
MENUFONT="" #size=10 font=UbuntuMono-Bold"
FONT=""
MAX_PING=1000
SITES=(google.com youtube.com wikipedia.org github.com)
#grab ping times for all sites
SITE_INDEX=0
PING_TIMES=
while [ $SITE_INDEX -lt ${#SITES[@]} ]; do
NEXT_SITE="${SITES[$SITE_INDEX]}"
if RES=$(ping -c 2 -n -q "$NEXT_SITE" 2>/dev/null); then
NEXT_PING_TIME=$(echo "$RES" | awk -F '/' 'END {printf "%.0f\n", $5}')
else
NEXT_PING_TIME=$MAX_PING
fi
if [ -z "$PING_TIMES" ]; then
PING_TIMES=($NEXT_PING_TIME)
else
PING_TIMES=(${PING_TIMES[@]} $NEXT_PING_TIME)
fi
SITE_INDEX=$(( SITE_INDEX + 1 ))
done
# Calculate the average ping
SITE_INDEX=0
AVG=0
while [ $SITE_INDEX -lt ${#SITES[@]} ]; do
AVG=$(( (AVG + ${PING_TIMES[$SITE_INDEX]}) ))
SITE_INDEX=$(( SITE_INDEX + 1 ))
done
AVG=$(( AVG / ${#SITES[@]} ))
# Calculate STD dev
SITE_INDEX=0
AVG_DEVS=0
while [ $SITE_INDEX -lt ${#SITES[@]} ]; do
AVG_DEVS=$(( AVG_DEVS + (${PING_TIMES[$SITE_INDEX]} - AVG)**2 ))
SITE_INDEX=$(( SITE_INDEX + 1 ))
done
AVG_DEVS=$(( AVG_DEVS / ${#SITES[@]} ))
SD=$(echo "sqrt ( $AVG_DEVS )" | bc -l | awk '{printf "%d\n", $1}')
if [ $AVG -ge $MAX_PING ]; then
MSG=" ☠ "
else
MSG='⌁'"$AVG"'±'"$SD"
fi
echo "$MSG | $MENUFONT"
echo "---"
SITE_INDEX=0
while [ $SITE_INDEX -lt ${#SITES[@]} ]; do
PING_TIME=${PING_TIMES[$SITE_INDEX]}
if [ $PING_TIME -eq $MAX_PING ]; then
PING_TIME="☠"
else
PING_TIME="$PING_TIME ms | $FONT"
fi
echo "${SITES[$SITE_INDEX]}: $PING_TIME"
SITE_INDEX=$(( SITE_INDEX + 1 ))
done
echo "---"
echo "Refresh... | refresh=true"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment