-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping_m
40 lines (36 loc) · 1020 Bytes
/
ping_m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
INTERVAL=5
HOST=${1:-8.8.8.8}
function resolveAddress(){
if [[ $1 =~ ^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$ ]]; then
echo "IP address $1"
else
echo -n "Checking IP address $1... "
IP=$(dig +short -4 "$1" A | grep -v "dig:" | grep -v ";;" || true)
if [ $(echo "${IP}" | grep "." | wc -l) -eq 1 ]; then
echo ${IP}
else
echo "can't resolve IP"
exit -1
fi
fi
}
resolveAddress "$HOST"
while true; do
COLS=$(echo "scale=1; (`tput cols` - 8) / 7.5" | bc)
PING=$(ping -c 1 -t $INTERVAL $HOST 2>&1 | grep 'icmp_seq' || true)
if [ -z "$PING" ]
then
TIME=$(echo "$INTERVAL*1000" | bc)
PRINT_TIME="99999"
else
TIME=$(echo $PING | cut -d'=' -f 4 | cut -d' ' -f 1 | cut -d'.' -f 1)
PRINT_TIME=$TIME
fi
printf '%8s %s' "${PRINT_TIME}ms"
EVAL=$(echo "scale=1;if($PRINT_TIME < 100) 1 else (l($PRINT_TIME) - 4) * $COLS" | bc -l | cut -d. -f 1)
seq -s "=" $EVAL | sed 's/[0-9]//g'
sleep $(echo "$INTERVAL - $TIME/1000" | bc -l)
done