Skip to content

Instantly share code, notes, and snippets.

@XFalko
Forked from jovimon/openvpn-telegram.md
Created April 20, 2022 05:01
Show Gist options
  • Save XFalko/d4397dab7f1d19ecc42c8d2ad42cc53c to your computer and use it in GitHub Desktop.
Save XFalko/d4397dab7f1d19ecc42c8d2ad42cc53c to your computer and use it in GitHub Desktop.
Telegram notifications for (dis)connections to my pfSense OpenVPN Server

OpenVPN Telegram Notifier

Features

Each time a client (dis)connects to the OpenVPN server of pfSense a new Telegram notification will be sent.

pfSense WebGUI configuration

Under "VPN" > "Servers" > your server > "Advanced Configuration" > "Custom Options" add the following lines.

client-connect    /root/openvpn-telegram.sh
client-disconnect /root/openvpn-telegram.sh
script-security 2

Script to be placed in the pfSense Box

Shell script to be created on the pfSense box. Access via SSH to the box and type nano /root/openvpn-telegram.sh Remember changing the <TOKEN> and the <CHAT_ID> variables with your own values.

#!/bin/sh
MYDATE=$(/bin/date +'%Y/%m/%d %H:%M:%S')
if [ "$script_type" = "client-connect" ]; then
  /usr/local/bin/curl -s -k "https://api.telegram.org/bot<TOKEN>/sendMessage" -d text="$MYDATE - VPN connection established. Username $common_name with external IP address $trusted_ip obtains internal IP address $ifconfig_pool_remote_ip." -d chat_id=<CHAT_ID>
elif  [ "$script_type" = "client-disconnect" ]; then
  /usr/local/bin/curl -s -k "https://api.telegram.org/bot<TOKEN>/sendMessage" -d text="$MYDATE - VPN connection terminated. Username $common_name with external IP address $trusted_ip frees internal IP address $ifconfig_pool_remote_ip." -d chat_id=<CHAT_ID>
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment