#!/bin/bash # $HeadURL$ $LastChangedRevision$ set -e PROGNAME=$(basename $0) # Load support functions . $(pcms-config PCMS_SHARE_PREFIX)/scripts/support.sh main() { # Defaults for options VERBOSELEVEL=2 OPT_TIMEOUT=5 # Process options while [ $# -ge 1 ]; do case $1 in --timeout=*) OPT_TIMEOUT=${1#*=} ;; --debug=*) VERBOSELEVEL=${1#*=} ;; -d) VERBOSELEVEL=$2; shift ;; -v|--verbose) VERBOSELEVEL=3 ;; --) shift; break ;; -*) error "main: $1: bad option" ;; *) break ;; esac shift done # Process arguments [ $# -ge 1 ] || usage # Guts ALL_OK=true for CMDLINE_ARG in "$@"; do case $CMDLINE_ARG in gateway) TARGET_HOSTNAMES=( $(ip -4 -o route | sed -nr 's/default via (.*) dev .*/\1/p') ) ;; internet) TARGET_HOSTNAMES=( 8.8.8.8 www.de ) ;; repos) TARGET_HOSTNAMES=( $(sed -nr 's/^deb https?:\/\/([^ /]+).*/\1/p' /etc/apt/sources.list /etc/apt/sources.list.d/* 2>/dev/null | sort -u | paste -d' ' -s) ) ;; *) TARGET_HOSTNAMES=( $CMDLINE_ARG ) ;; esac for TARGET_HOSTNAME in "${TARGET_HOSTNAMES[@]}"; do debug 10 "$TARGET_HOSTNAME: checking ..." STOP_TIMESTAMP=$(($(date +%s) + $OPT_TIMEOUT)) while :; do NOW_TIMESTAMP=$(date +%s) debug 10 "main: NOW_TIMESTAMP=$NOW_TIMESTAMP, STOP_TIMESTAMP=$STOP_TIMESTAMP" if ping -c 1 -w 0.2 $TARGET_HOSTNAME > /dev/null 2>/dev/null; then OK=true break fi if (($NOW_TIMESTAMP >= $STOP_TIMESTAMP)); then OK=false break fi done $OK || { ALL_OK=false; break 2; } done done # CMDLINE_ARG will still contain the last failed hostname or alias, so we # can use it in an error message. $ALL_OK || error "$CMDLINE_ARG: not reachable or resolution problem (depending how caller handles this, it may be okay)" } usage() { echo "Usage: $PROGNAME [ --timeout= ] { gateway | repos | internet | } ..." >&2; exit 1; } main "$@"