#!/data/data/com.termux/files/usr/bin/bash
# ping: how fast a few sites answer. Green is quick, yellow slow, red down.
# Put your own servers in HOSTS.
# termux.zanoski.com/widgets
HOSTS="1.1.1.1 google.com github.com"
e=$'\e'; R="${e}[0m"
for h in $HOSTS; do
  t=$(curl -o /dev/null -s -m 4 -w '%{time_connect}' "https://$h" 2>/dev/null)
  ms=$(awk -v t="${t:-0}" 'BEGIN{printf "%d", t * 1000}')
  if [ "$ms" -le 0 ]; then printf "${e}[31m● %-12s down$R\n" "$h"; continue; fi
  c=32; [ $ms -gt 80 ] && c=33; [ $ms -gt 200 ] && c=31
  printf "${e}[${c}m● %-12s %4d ms$R\n" "$h" $ms
done
