Skip to content

Instantly share code, notes, and snippets.

@andreasottosson
Last active June 29, 2021 18:31
Show Gist options
  • Save andreasottosson/5a4f5e3890c609b589509f43065a35c3 to your computer and use it in GitHub Desktop.
Save andreasottosson/5a4f5e3890c609b589509f43065a35c3 to your computer and use it in GitHub Desktop.
WireGuard up/down check in bash
#!/bin/bash
# Make sure WireGuard is up on server
# Check twice with 15s delay
# Send notification if WireGuard is down
TOKEN=
USER=
COUNT=0
for round in 1 2
do
WG_IP=$(curl -s --interface wg0 ifconfig.io)
IP=$(curl -s ifconfig.io)
if [ "$WG_IP" == "$IP" ] || [ "$WG_IP" == "" ]
then
((COUNT++))
echo "IP is empty or matching!!!"
(date && echo "Check "$COUNT"/2. Wireguard IP is $WG_IP.") >> /var/log/check_wireguard.log
if [ "$COUNT" -eq 2 ]
then
RESPONSE=$(curl -s --form-string "token=$TOKEN" --form-string "user=$USER" --form-string "message=WireGuard is down on server!" https://api.pushover.net/1/messages.json)
echo "Notification sent!"
(date && echo "$RESPONSE") >> /var/log/check_wireguard.log
exit 1
fi
sleep 15
else
echo "IP is not the same. Exiting..."
exit 0
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment