ping-out/ping-host.sh
Eliezer Croitoru 16aad23d12 1
2024-11-19 18:26:20 +02:00

24 lines
676 B
Bash
Executable File

#!/usr/bin/env bash
# Ping command line options:
# -c: stop after c answered packages
# -i: seconds wait interval
# -q quiet
# -w: seconds overall deadline
DESTINATION="$1"
SECONDS="2"
RATE_IN_SECONDS="2"
NUMBER_OF_PINGS="1"
INTERVAL="1"
while sleep ${RATE_IN_SECONDS}; do
ping_output="$(ping -c ${NUMBER_OF_PINGS} -i ${INTERVAL} -q -w ${SECONDS} ${DESTINATION} 2>/dev/null)"
PING_EXIT_CODE="$?"
if [ "${PING_EXIT_CODE}" -ne 0 ]; then
ping_results="$(grep 'packets transmitted.*received.*packet loss.*time' <<< "$ping_output")"
now="$(date '+%s') $(date)"
printf "%s %s %s\n" "$now" "$ping_exit_code" "$ping_results"
fi
done