DEV Community

Cover image for Wireguard client connection/disconnection notification
alfiosalanitri
alfiosalanitri

Posted on • Edited on

3 2

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

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay