Each time a client (dis)connects to the OpenVPN server of pfSense a new Telegram notification will be sent.
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
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
Hello @patrickpesegodinski
I deployed it on an earlier version of pfsense and have since then updated to 2.6.0 and it still works.
Maybe it has to do with setting the script as executable as the other commenter said?
Thanks for using the script, hope it helps.