scripts/ssh_tools/multiping.sh

65 lines
1.5 KiB
Bash
Executable File

#!/bin/sh
#
# multiping.sh, Copyright © 2013, 2019 Matteo Cypriani <mcy@lm7.fr>
# (Formerly named cluster-ping.sh)
#
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
#
# Ping a list of remote hosts using fping.
#set -x
set -u
readonly EXIT_USAGE=127
readonly EXIT_HOSTS_LIST=6
err()
{
printf 'Error! ' >&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
if [ $# -ne 1 ] ; then
echo "Usage: $0 <hosts_list>" >&2
exit $EXIT_USAGE
fi
FPING=$(command -v fping)
[ -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"
HOSTS="$XDG_CONFIG_HOME/ssh_tools/$1.lst"
echo "Using file '$HOSTS' as hosts list."
if [ ! -f "$HOSTS" ] ; then
err "The hosts list file doesn't exist or is not a regular file."
exit $EXIT_HOSTS_LIST
fi
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