-
Notifications
You must be signed in to change notification settings - Fork 4
/
eos-connection-checker
executable file
·55 lines (48 loc) · 1.71 KB
/
eos-connection-checker
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
#!/bin/bash
#
# Checks that an internet connection exists.
# Return value is 0 on success, or another number on failure (see also: 'curl-exit-code-to-string').
#
# Example usage:
# eos-connection-checker && echo connection is ON
# eos-connection-checker || echo connection is OFF
# eos-connection-checker || curl-exit-code-to-string $?
# FastestMirror assumes the list was created with eos-rankmirrors.
# Both functions below return the URLs in format: https://mirror.moson.org/endeavouros/repo/state
FastestMirror() { grep -m1 "^# https://" $ml | sed 's|# \([^$]*\)/\$.*|\1/state|' ; }
AllMirrors() { grep "^Server[ ]*=[ ]*https://" $ml | sed 's|^Server[ ]*=[ ]*\([^$]*\)/\$.*|\1/state|' ; }
Test() {
local url="$1"
/usr/bin/curl --silent --fail --connect-timeout 8 "$url" >/dev/null # download the 'state' file from a mirror
retval=$?
if [ $retval -eq 0 ] ; then
[ "$verbose" = "yes" ] && echo "Mirror = $url" >&2
exit 0 # connection exists
fi
}
Main() {
local -r ml=/etc/pacman.d/endeavouros-mirrorlist
local fastest=""
local others=""
local URL
local retval=201
local verbose=no
local fallback="https://forum.endeavouros.com/faq" # fallback if no mirrors in $ml
case "$1" in
-v | --verbose) verbose=yes ;; # will show the responding mirror URL
esac
if [ -e $ml ] ; then
fastest="$(FastestMirror)"
if [ "$fastest" ] ; then
Test "$fastest"
others="$(AllMirrors | grep -v "$fastest")"
else
others="$(AllMirrors)"
fi
fi
for URL in $others $fallback ; do
Test "$URL"
done
return $retval # no connection
}
Main "$@"