Skip to content

Instantly share code, notes, and snippets.

@shvchk
Last active June 14, 2022 17:45
Show Gist options
  • Select an option

  • Save shvchk/a64b1d7420d887c2451d32e957fb8df3 to your computer and use it in GitHub Desktop.

Select an option

Save shvchk/a64b1d7420d887c2451d32e957fb8df3 to your computer and use it in GitHub Desktop.
#! /usr/bin/env sh
host=""
wait=10
max_tries=3
up_cmd=""
down_cmd=""
# --------------------
log="logger -t wg-if-up"
host_status="down"
try=1
err=""
[ -n "$host" ] || err="No host specified"
[ -n "$up_cmd" ] || err="No up command specified"
[ -n "$down_cmd" ] || err="No down command specified"
[ -z "$err" ] || { $log "$err"; exit 1; }
while :; do
case $host_status in
up)
while ping -c 1 -W 1 $host > /dev/null; do
try=1
sleep $wait
done
$log "No connection to $host detected (try $try)"
if [ $try -lt $max_tries ]; then
try=$(( try + 1 ))
continue
fi
host_status="down"
$down_cmd
;;
down)
until ping -c 1 -W 1 $host > /dev/null; do
sleep $wait
done
try=1
host_status="up"
$up_cmd
;;
esac
$log "Host $host $host_status"
sleep $wait
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment