diff --git a/ssh_tools/multiping.sh b/ssh_tools/multiping.sh index 8dbccb6..5a80dc1 100755 --- a/ssh_tools/multiping.sh +++ b/ssh_tools/multiping.sh @@ -15,7 +15,6 @@ set -u readonly EXIT_USAGE=127 -readonly EXIT_DEPENDENCY=4 readonly EXIT_HOSTS_LIST=6 err() @@ -24,6 +23,20 @@ err() printf '%s\n' "$@" >&2 } +warn() +{ + printf 'Warning! ' >&2 + printf '%s\n' "$@" >&2 +} + +ping_hosts() +{ + grep -v '^#' "$HOSTS" | while read -r host ; do + [ -z "$host" ] && continue + ping -c 1 -W 1 "$host" >/dev/null 2>&1 && echo "$host is alive" + done +} + # Check arguments if [ $# -ne 1 ] ; then echo "Usage: $0 " >&2 @@ -31,10 +44,7 @@ if [ $# -ne 1 ] ; then fi FPING=$(command -v fping) -if [ -z "$FPING" ] ; then - err "fping is required for this script to work." - exit $EXIT_DEPENDENCY -fi +[ -z "$FPING" ] && warn "fping is not available. Install it for best results." # Hosts list file [ -z ${XDG_CONFIG_HOME+x} ] && XDG_CONFIG_HOME="$HOME/.config" @@ -45,6 +55,10 @@ if [ ! -f "$HOSTS" ] ; then exit $EXIT_HOSTS_LIST fi -# Go! -# shellcheck disable=SC2046 -exec "$FPING" $(cat "$HOSTS") 2>/dev/null +if [ -n "$FPING" ] ; then + # shellcheck disable=SC2046 + exec "$FPING" $(cat "$HOSTS") 2>/dev/null +fi + +# Fall back to good old ping if fping is not available +ping_hosts