Created
April 29, 2022 11:01
-
-
Save 0x5e/85d2dee9bb2c5bea5e74c5fdc72be11c to your computer and use it in GitHub Desktop.
wireguard watchdog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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