Skip to content

Instantly share code, notes, and snippets.

@0x5e
Created April 29, 2022 11:01
Show Gist options
  • Save 0x5e/85d2dee9bb2c5bea5e74c5fdc72be11c to your computer and use it in GitHub Desktop.
Save 0x5e/85d2dee9bb2c5bea5e74c5fdc72be11c to your computer and use it in GitHub Desktop.
wireguard watchdog
#!/bin/bash
#
# This watchdog script restart systemctl wireguard service if ping timeout for 3 times
#
# Usage:
# ./wg-watchdog.sh [interface] [ip]
#
# Example:
# ./wg-watchdog.sh wg0 192.168.40.1
#
# Example in Crontab:
# */10 * * * * /root/tools/wg-watchdog.sh wg0 192.168.40.1
#
check_peer_activity() {
interface=$1
peer=$2
tries=0
while [[ $tries -lt 3 ]]
do
if ping -c 1 -W 3 $peer > /dev/null 2>&1
then
echo "[wg-watchdog] $interface $peer connected"
return
fi
echo "[wg-watchdog] $interface $peer timeout"
tries=$((tries+1))
done
echo "[wg-watchdog] restarting $interface"
systemctl restart "wg-quick@$interface"
}
check_peer_activity "$1" "$2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment