Skip to content

Instantly share code, notes, and snippets.

@jovimon
Last active February 18, 2024 07:08
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save jovimon/8f480cb0621d74b4e993ff7a9bede215 to your computer and use it in GitHub Desktop.
Save jovimon/8f480cb0621d74b4e993ff7a9bede215 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
@XFalko
Copy link

XFalko commented Jul 22, 2021

With pfSense 2.5.2 and 21.05 , openvpn configuration doesn't works if doesn't set script as executable ( chmod +x /root/openvpn-telegram.sh )

@patrickpesegodinski
Copy link

It's workin in the 2.6.0 version?

Because the error is generate in the log OpenVPN: WARNING: Failed running command (--client-connect): could not execute external program

@jovimon
Copy link
Author

jovimon commented May 15, 2023

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.

@patrickpesegodinski
Copy link

patrickpesegodinski commented Jul 18, 2023

Hello @jovimon

Yes, I setting the chmod +x in the script.

@patrickpesegodinski
Copy link

Hello @jovimon

I achieved run the script.

One question: It's possible add the variable to identify the name to server OPENVPN?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment