[ssh_tools] multiping: don't require fping
This commit is contained in:
parent
a2f5e9a48a
commit
665ca65cd2
|
@ -15,7 +15,6 @@
|
||||||
set -u
|
set -u
|
||||||
|
|
||||||
readonly EXIT_USAGE=127
|
readonly EXIT_USAGE=127
|
||||||
readonly EXIT_DEPENDENCY=4
|
|
||||||
readonly EXIT_HOSTS_LIST=6
|
readonly EXIT_HOSTS_LIST=6
|
||||||
|
|
||||||
err()
|
err()
|
||||||
|
@ -24,6 +23,20 @@ err()
|
||||||
printf '%s\n' "$@" >&2
|
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
|
# Check arguments
|
||||||
if [ $# -ne 1 ] ; then
|
if [ $# -ne 1 ] ; then
|
||||||
echo "Usage: $0 <hosts_list>" >&2
|
echo "Usage: $0 <hosts_list>" >&2
|
||||||
|
@ -31,10 +44,7 @@ if [ $# -ne 1 ] ; then
|
||||||
fi
|
fi
|
||||||
|
|
||||||
FPING=$(command -v fping)
|
FPING=$(command -v fping)
|
||||||
if [ -z "$FPING" ] ; then
|
[ -z "$FPING" ] && warn "fping is not available. Install it for best results."
|
||||||
err "fping is required for this script to work."
|
|
||||||
exit $EXIT_DEPENDENCY
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Hosts list file
|
# Hosts list file
|
||||||
[ -z ${XDG_CONFIG_HOME+x} ] && XDG_CONFIG_HOME="$HOME/.config"
|
[ -z ${XDG_CONFIG_HOME+x} ] && XDG_CONFIG_HOME="$HOME/.config"
|
||||||
|
@ -45,6 +55,10 @@ if [ ! -f "$HOSTS" ] ; then
|
||||||
exit $EXIT_HOSTS_LIST
|
exit $EXIT_HOSTS_LIST
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Go!
|
if [ -n "$FPING" ] ; then
|
||||||
# shellcheck disable=SC2046
|
# shellcheck disable=SC2046
|
||||||
exec "$FPING" $(cat "$HOSTS") 2>/dev/null
|
exec "$FPING" $(cat "$HOSTS") 2>/dev/null
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Fall back to good old ping if fping is not available
|
||||||
|
ping_hosts
|
||||||
|
|
Loading…
Reference in New Issue