forked from Noah-Huppert/net-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net-test.sh
executable file
·71 lines (63 loc) · 1.52 KB
/
net-test.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
67
68
69
70
71
#!/usr/bin/env bash
#
#?
# Net Test - Monitors network connectivity for downtime.
#
# Usage: net-test.sh
#
# Attempts to connect to an internet service to verify internet connectivity
# every second. Prints the status of this check in the format:
#
# <Unix Time> <Internet Connectivity> <Fallback Number> <Ping Time>
#
# Where <Internet Connectivity> is a 0 or a 1. And <Fallback Number> is the
# index of the site in test_sites which the internet connectivity status was
# determined with.
#
# Arguments
# --no-header: Makes script not print sites header
#?
# List of sites to test internet connectivity with
test_sites=("1.1.1.1" "8.8.8.8" "google.com" "wikipedia.com")
test_interval=1
# Arguments
op_no_header="false"
while [ ! -z "$1" ]; do
key="$1"
shift
case "$key" in
--no-header)
op_no_header="true"
;;
*)
echo "Error: unknown argument \"$key\"" >&2
exit 1
;;
esac
done
# Print site names
if [ "$op_no_header" != "true" ]; then
for site in "${test_sites[@]}"; do
echo "#$site"
done
fi
# Check
while true; do
current_time="$(date +%s)"
internet_conn=0
fallback_num=0
ping_time="-1"
# Try each test site until one succeeds
for site in "${test_sites[@]}"; do
ping_time_out=$((ping -c 1 "$site" | tail -1 | awk '{print $4}' | cut -d '/' -f 2) 2> /dev/null)
if [ ! -z "$ping_time_out" ]; then
internet_conn=1
ping_time="$ping_time_out"
break
fi
fallback_num=$(("$fallback_num" + 1))
done
# Record
echo "$current_time $internet_conn $fallback_num $ping_time"
sleep "$test_interval"
done