[ssh_tools] multiping: don't require fping

This commit is contained in:
Matteo Cypriani 2019-10-21 18:06:48 +02:00
parent a2f5e9a48a
commit 665ca65cd2
1 changed files with 22 additions and 8 deletions

View File

@ -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 <hosts_list>" >&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