Skip to content

Instantly share code, notes, and snippets.

@namxam
Forked from mattkasun/wg-watchdog.sh
Last active February 23, 2023 06:57
Show Gist options
  • Save namxam/784aafd29fa3554c8bb3c40dc25ac314 to your computer and use it in GitHub Desktop.
Save namxam/784aafd29fa3554c8bb3c40dc25ac314 to your computer and use it in GitHub Desktop.
wireguard watchdog script
#!/bin/bash
# 1. Save file to system /usr/bin
# 2. Make it executable
# 3. Add crontab entry:
# sudo su
# crontab -e
# Add `* * * * * /usr/bin/wg-watchdog`
#
# You can track history via `journalctl -f -t wg-watchdog`
tries=0
while [[ $tries -lt 3 ]]
do
# Ping wireguard vpn gateway
if /bin/ping -c 1 10.254.0.1
then
# Notify that we are online
logger -i -t "wg-watchdog" -p user.notice "wireguard online"
exit 0
fi
# Notify that we are offline
logger -i -t "wg-watchdog" -p user.notice "wireguard offline"
tries=$((tries+1))
done
# Restart service to reconnect
sudo systemctl restart wg-quick@wg0
logger -i -t "wg-watchdog" -p user.notice "wireguard restarted"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment