massping script

just a little script which pings a defined number of servers a defined time and give you the result. Good for looking if you connection is stable or if it may be an internet problem.

#!/bin/bash

targets=( 1.1.1.1 8.8.8.8 cloudflare.com discord.com )
max=100


tmpfile=$$.massping.tmp

# functions
f_ping(){
  v=$(ping -c $max $1 2>/dev/null|grep received|cut -d' ' -f4)
  echo $1 $v >> $tmpfile
}

f_done(){
  [[ ! -f $tmpfile ]] && echo false && return
  [[ $(wc -l $tmpfile 2>/dev/null|cut -d' ' -f1) -eq ${#targets[@]} ]] && echo true && return
  echo false
}

# main
for s in ${targets[@]}; do
  f_ping $s &
done

c=$max; echo
while [[ $(f_done) == false ]]; do
  echo -e "\e[1A\e[Kwaiting. This should need $c more seconds.."
  c=$(( $c - 1 ))
  sleep 1
done
echo -e "\e[1A\e[Kdone"; echo

maxline=0; for w in $(cat $tmpfile|cut -d' ' -f1); do [[ ${#w} -gt $maxline ]] && maxline=${#w}; done
spaces=""; while [[ $(( ${#spaces} + 1 )) -lt $maxline ]]; do spaces="$spaces "; done
echo "server${spaces}%"
IFS="
"
while read -r line; do
  server=$(echo $line|cut -d' ' -f1)
  count=$(echo $line|cut -d' ' -f2)
  perc=$(echo "scale=1; $count * 100 / $max"|bc)
  spaces=""; while [[ $(( ${#spaces} + ${#server} - ( 5 - ${#perc} ) )) -lt $maxline ]]; do spaces="$spaces "; done
  echo "${server}${spaces} ${perc}"
done < $tmpfile

[[ -f $tmpfile ]] && rm -f $tmpfile

exampleoutput:

server             %
1.1.1.1        100.0
8.8.8.8        100.0
cloudflare.com  99.0
discord.com     85.0

Leave a Reply

Your email address will not be published. Required fields are marked *