Pfsense telegram notification on openvpn access: Difference between revisions

From Fvettore-WIKI
Created page with "== To receive Telegram notifications for OpenVPN connections/disconnections == In the following example: *we have two bot user chatIds (Fabrizio and Banion), respectively 173072004 and 5704842034.</br> *the BOT API key is: 1757914630:AAFw1EE42wCTJ4Vy5HTUtRUb34BQna6nU3I (not a real value, example only)</br> </br> You need to create a bash script named ''openvpn-telegram.sh'' in the pfsense root and give it execution permissions <pre> #!/bin/sh MYDATE=$(/bin/date +'%Y/%m..."
 
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 2: Line 2:
In the following example:
In the following example:


*we have two bot user chatIds (Fabrizio and Banion), respectively 173072004 and 5704842034.</br>
* we have two bot user chatIds (Fabrizio and Banion), respectively 173072004 and 5704842034.</br>
*the BOT API key is: 1757914630:AAFw1EE42wCTJ4Vy5HTUtRUb34BQna6nU3I (not a real value, example only)</br>
* the BOT API key is: 1757914630\:AAFw1EE42wCTJ4Vy5HTUtRUb34BQna6nU3I (not a real value, example only)</br>
 
</br> 
You need to create a bash script named ''openvpn-telegram.sh'' in the pfsense root and give it execution permissions 
<pre>
#!/bin/sh
MYDATE=$(/bin/date +'%Y/%m/%d %H:%M:%S')
if [ "$script_type" = "client-connect" ]; then
  /usr/local/bin/curl  -d "chat_id=173072004" -d "text=$MYDATE - VPN connection connected. Username $common_name with external IP address $trusted_ip obtains internal IP address
$ifconfig_pool_remote_ip."  "https://api.telegram.org/bot1757914630:AAFw1EE42wCTJ4Vy5HTUtRUb34BQna6nU3I/sendMessage"
  /usr/local/bin/curl  -d "chat_id=5704842034" -d "text=$MYDATE - VPN connection connected. Username $common_name with external IP address $trusted_ip obtains internal IP address
$ifconfig_pool_remote_ip."  "https://api.telegram.org/bot1757914630:AAFw1EE42wCTJ4Vy5HTUtRUb34BQna6nU3I/sendMessage"
elif  [ "$script_type" = "client-disconnect" ]; then
  /usr/local/bin/curl  -d "chat_id=173072004" -d "text=$MYDATE - VPN connection disconnected. Username $common_name "
"https://api.telegram.org/bot1757914630:AAFw1EE42wCTJ4Vy5HTUtRUb34BQna6nU3I/sendMessage"
  /usr/local/bin/curl  -d "chat_id=5704842034" -d "text=$MYDATE - VPN connection disconnected. Username $common_name "
"https://api.telegram.org/bot1757914630:AAFw1EE42wCTJ4Vy5HTUtRUb34BQna6nU3I/sendMessage"
fi
exit 0
</pre>


</br> You need to create a bash script named ''openvpn-telegram.sh'' in the pfsense root and give it execution permissions <pre> #!/bin/sh MYDATE=$(/bin/date +'%Y/%m/%d %H:%M:%S') if [ "$script_type" = "client-connect" ]; then /usr/local/bin/curl -d "chat_id=173072004" -d "text=$MYDATE - VPN connection connected. Username $common_name with external IP address $trusted_ip obtains internal IP address $ifconfig_pool_remote_ip." "https://api.telegram.org/bot1757914630:AAFw1EE42wCTJ4Vy5HTUtRUb34BQna6nU3I/sendMessage" /usr/local/bin/curl -d "chat_id=5704842034" -d "text=$MYDATE - VPN connection connected. Username $common_name with external IP address $trusted_ip obtains internal IP address $ifconfig_pool_remote_ip." "https://api.telegram.org/bot1757914630:AAFw1EE42wCTJ4Vy5HTUtRUb34BQna6nU3I/sendMessage" elif [ "$script_type" = "client-disconnect" ]; then /usr/local/bin/curl -d "chat_id=173072004" -d "text=$MYDATE - VPN connection disconnected. Username $common_name " "https://api.telegram.org/bot1757914630:AAFw1EE42wCTJ4Vy5HTUtRUb34BQna6nU3I/sendMessage" /usr/local/bin/curl -d "chat_id=5704842034" -d "text=$MYDATE - VPN connection disconnected. Username $common_name " "https://api.telegram.org/bot1757914630:AAFw1EE42wCTJ4Vy5HTUtRUb34BQna6nU3I/sendMessage" fi exit 0 </pre>
In the advanced configuration of each server in the ''custom options'' field:
In the advanced configuration of each server in the ''custom options'' field:


<pre> client-connect /root/openvpn-telegram.sh client-disconnect /root/openvpn-telegram.sh script-security 2 </pre>
<pre>
client-connect   /root/openvpn-telegram.sh
client-disconnect /root/openvpn-telegram.sh
script-security 2
</pre>
[[Category:OpenVPN]][[Category:Telegram]][[Category:Pfsense]]

Latest revision as of 09:02, 31 July 2025

To receive Telegram notifications for OpenVPN connections/disconnections

In the following example:

  • we have two bot user chatIds (Fabrizio and Banion), respectively 173072004 and 5704842034.
  • the BOT API key is: 1757914630\:AAFw1EE42wCTJ4Vy5HTUtRUb34BQna6nU3I (not a real value, example only)


You need to create a bash script named openvpn-telegram.sh in the pfsense root and give it execution permissions

 #!/bin/sh
 MYDATE=$(/bin/date +'%Y/%m/%d %H:%M:%S')
 if [ "$script_type" = "client-connect" ]; then
  /usr/local/bin/curl  -d "chat_id=173072004" -d "text=$MYDATE - VPN connection connected. Username $common_name with external IP address $trusted_ip obtains internal IP address 
 $ifconfig_pool_remote_ip."   "https://api.telegram.org/bot1757914630:AAFw1EE42wCTJ4Vy5HTUtRUb34BQna6nU3I/sendMessage"
  /usr/local/bin/curl  -d "chat_id=5704842034" -d "text=$MYDATE - VPN connection connected. Username $common_name with external IP address $trusted_ip obtains internal IP address 
 $ifconfig_pool_remote_ip."   "https://api.telegram.org/bot1757914630:AAFw1EE42wCTJ4Vy5HTUtRUb34BQna6nU3I/sendMessage"
 elif  [ "$script_type" = "client-disconnect" ]; then
  /usr/local/bin/curl  -d "chat_id=173072004" -d "text=$MYDATE - VPN connection disconnected. Username $common_name " 
 "https://api.telegram.org/bot1757914630:AAFw1EE42wCTJ4Vy5HTUtRUb34BQna6nU3I/sendMessage"
  /usr/local/bin/curl  -d "chat_id=5704842034" -d "text=$MYDATE - VPN connection disconnected. Username $common_name " 
 "https://api.telegram.org/bot1757914630:AAFw1EE42wCTJ4Vy5HTUtRUb34BQna6nU3I/sendMessage"
 fi
 exit 0

In the advanced configuration of each server in the custom options field:

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