-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzaping.sh
executable file
·66 lines (56 loc) · 1.82 KB
/
zaping.sh
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash
#
# Options:
#
# zaping.sh server [mode=avg] [count=5] [interval=0.2]
#
# server Server to ping, either ip or dns
# option operational mode (default avg)
# "loss" to get the percentage of lost pings in a range of pings
# "min" to get the minimum time in a range of pings
# "avg" to get the average time
# "max" to get the max time
# count How many times to ping when doing a range of pings
# interval Interval between pings during a range. Must be
# greater than 0.2 (only root can go less than 0.2)
#
# Returns positive floating point number on success and -1 on failure
# Maximum rtt is limited to 1s
set -o pipefail
MAXAGE=60
[ $# -eq 0 ] && exit
host="$1"
mode="avg"
count=5
interval=0.2
[ "$2" != "" ] && mode="$2"
[ "$3" != "" ] && count="$3"
[ "$4" != "" ] && interval="$4"
DATAFILE="/tmp/zaping-$host-$count-$interval"
if [ -f "$DATAFILE" ]; then
modsecs=$(date --utc --reference="$DATAFILE" +%s)
nowsecs=$(date +%s)
delta=$((nowsecs-modsecs))
else
delta=999
fi
if [ $delta -gt $MAXAGE ]; then
wrkfile=`mktemp`
ping -c $count -i $interval -W 1 "$host" > "$wrkfile" 2>/dev/null
loss=`grep loss "$wrkfile" | cut -f 6 -d" " | cut -f1 -d"%" || echo -1`
min=`grep rtt "$wrkfile" | cut -f 4 -d" " | cut -f 1 -d"/" || echo -1`
avg=`grep rtt "$wrkfile" | cut -f 4 -d" " | cut -f 2 -d"/" || echo -1`
max=`grep rtt "$wrkfile" | cut -f 4 -d" " | cut -f 3 -d"/" || echo -1`
rm "$wrkfile"
echo "loss=$loss" > "$DATAFILE"
echo "min=$min" >> "$DATAFILE"
echo "avg=$avg" >> "$DATAFILE"
echo "max=$max" >> "$DATAFILE"
fi
. "$DATAFILE"
case "$mode" in
loss) echo $loss ;;
min) echo $min ;;
avg) echo $avg ;;
max) echo $max ;;
esac