#!/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'). # # Examples: # eos-connection-checker -t=20 && echo connection exists || echo no connection # 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" /bin/curl --silent --fail --connect-timeout $timeout "$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 } CheckTimeout() { case "$timeout" in "") timeout=$default_timeout; return ;; # must have a value 0) timeout=1; return ;; # don't allow zero timeout 0*) timeout=$(echo "$timeout" | sed -E 's|[0]+(.+)|\1|') ;; # remove leading zeros esac [ "${timeout//[0-9]/}" ] && timeout=$default_timeout # only numbers allowed } Help() { local progname=${0##*/} cat < Timeout = $timeout" >&2 if [ -e $ml ] ; then fastest="$(FastestMirror)" # Try the fastest ranking mirror first. if [ "$fastest" ] ; then Test "$fastest" URLs=($(AllMirrors | grep -v "$fastest")) # Fastest failed, use the actual mirrorlist without then $fastest. else URLs=("$(AllMirrors)") # Fastest not found, use the actual mirrorlist. fi fi URLs+=("$fallback") # Fallback URL if no mirrors configured. for URL in "${URLs[@]}" ; do Test "$URL" done exit $retval # all tested URLs failed, exit with the last curl error } Main "$@"