#! /usr/bin/env bash # Send an ICMP echo request to each of the IPs given. # # Copyright (C) 2011-2022 Erik Auerswald # # Copying and distribution of this file, with or without modification, # are permitted in any medium without royalty provided the copyright # notice and this notice are preserved. This file is offered as-is, # without any warranty. # # Requires the ping program of Solaris or an output-compatible one. VERSION=2022-11-25-01 PROG="$(basename "$0")" PING="${PING_WRAPPER:-ping}" version() { echo "$PROG version $VERSION" echo "Copyright (c) 2011-2022 by Erik Auerswald " } usage() { echo "Usage: $PROG [-h] [-L] [-V] [-t TIMEOUT] [-v] [-u] [IP...]"; } help() { version usage cat <<-EOH Options: -h print this help and exit -t TIMEOUT specify the timeout value used for ping (default ${TIMEOUT}s) -v print additional runtime info -u print not reachable hosts instead -L print license and exit -V print version information and exit $PROG sends ICMP echo requests to the hosts (IP addresses) specified and prints every host that answers with an ICMP echo reply. Specify IP addresses as command line arguments. If no IP addresses are given as arguments, IP addresses are read from STDIN, one IP address per line. With the -u option, only hosts / IP addresses that do not answer are printed. EOH } license() { cat <<-EOL Copying and distribution of this file, with or without modification, are permitted in any medium without royalty provided the copyright notice and this notice are preserved. This file is offered as-is, without any warranty. EOL } TIMEOUT=1 VERB=0 UNREACHABLE=0 while getopts t:hvuLV OPT; do case "$OPT" in t) TIMEOUT=${OPTARG:-2};; h) help; exit 0;; v) VERB=1;; u) UNREACHABLE=1;; L) version; license; exit 0;; V) version; exit 0;; ?) usage; exit 1;; esac done shift $((OPTIND - 1)) test "$#" -eq 0 && test "${VERB}" -eq 1 && echo 'reading IP addresses from STDIN, one IP address per line' test "${VERB}" -eq 1 && echo "Sending ICMP echo requests and waiting for results..." \ "(timeout ${TIMEOUT}s)" test \( "${VERB}" -eq 1 \) -a \( "${UNREACHABLE}" -eq 1 \) && echo "Printing not reachable hosts." PATTERN='is alive' test "${UNREACHABLE}" -eq 1 && PATTERN='no answer from' if test "$#" -gt 0; then while test $# -ge 1; do { $PING -n "$1" "$TIMEOUT" | grep -F "$PATTERN"; } & shift done else while read -r IP; do { $PING -n "$IP" "$TIMEOUT" | grep -F "$PATTERN"; } & done fi | sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n -u exit 0