DEV Community

Cover image for Wireguard client connection/disconnection notification
alfiosalanitri
alfiosalanitri

Posted on • Edited on

Wireguard client connection/disconnection notification

With this tutorial you can setup your server to send an alert to telegram when a client is connected or disconnected from the tunnel.

The script sends a message to telegram if the client hasn't activity with the server for x (15 default) minutes.

Prerequisites

  • Server with Wireguard and root privileges (read this post to install wireguard)
  • curl sudo apt install curl
  • Telegram Bot (how to create a bot)

Installation

TIME_LIMIT=15 (alert will be sent after 15 minutes of inactivity)
TELEGRAM_CHAT_ID="your-chat-id"
TELEGRAM_BOT_ID="your-bot-api-key"
Enter fullscreen mode Exit fullscreen mode
  • Add execution privilege to wg-clients-guardian.sh
sudo -s
chmod +x /path/to/wireguard-client-connection-notification/wg-clients-guardian.sh
Enter fullscreen mode Exit fullscreen mode
  • create the cron job
* * * * * cd /path/to/wireguard-client-connection-notification/wg-clients-guardian.sh && /path/to/wireguard-client-connection-notification/wg-clients-guardian.sh > /dev/null 2>&1
Enter fullscreen mode Exit fullscreen mode

Do you want to receive an email instead of a telegram notification?

Change this lines: 98,99

MESSAGE="🐉 Wireguard: \`$CLIENT_NAME $SEND_NOTIFICATION from $REMOTE_IP\`"
        curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_ID}/sendMessage" -F chat_id=$TELEGRAM_CHAT_ID -F text="$MESSAGE" -F parse_mode="MarkdownV2" > /dev/null 2>&1
Enter fullscreen mode Exit fullscreen mode

whit

MESSAGE="Wireguard: $CLIENT_NAME $SEND_NOTIFICATION from $REMOTE_IP"
echo "$MESSAGE" | mail -s subject your@email.com
Enter fullscreen mode Exit fullscreen mode

Note: requires mail package sudo apt install mail

Top comments (0)