Netgate Discussion Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Search
    • Register
    • Login

    OpenVPN notification on connect and disconnect

    OpenVPN
    6
    6
    3.3k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • W
      willi25
      last edited by

      Hello,

      Since i am working with domoticz home automation and i figured out the telegram notifications (which works really great) i would like to get a notification in telegram on connect and disconnect of my openvpn. I found already some descriptions of notifications by email:
      https://forum.pfsense.org/index.php?topic=97267.msg541709#msg541709
      https://www.reddit.com/r/PFSENSE/comments/6yuisa/is_it_possible_to_setup_email_notifications_when/

      As i can send a notification to telegram via http-call i put the command in the /usr/local/sbin/openvpn.attributes.sh in the connect- and disconnect-section. But it doesn't work. The connection is established normally, but no notification.

      This is the file:

      echo $script_type > /tmp/script
      if [ "$script_type" = "client-connect" ]; then
      	if [ -f /tmp/$common_name ]; then
      		/bin/cat /tmp/$common_name > $1
      		/bin/rm /tmp/$common_name
      		/usr/local/bin/curl https://api.telegram.org/bot-my-personal-api-key/sendMessage?chat_id=xxx&text=VPNCONNECT
      ${trusted_ip} vpn_client_ip: ${ifconfig_pool_remote_ip} datetime: $(date +'%Y/%m/%d %H:%M:%S') ${common_name} has connected to OpenVPN
      		fi
      elif [ "$script_type" = "client-disconnect" ]; then
      	command="/sbin/pfctl -a 'openvpn/$common_name' -F rules"
      	eval $command
      	/sbin/pfctl -k $ifconfig_pool_remote_ip
      	/sbin/pfctl -K $ifconfig_pool_remote_ip
      	/usr/local/bin/curl https://api.telegram.org/bot-my-personal-api-key/sendMessage?chat_id=xxx&text=VPNDISCONNECT
      ${trusted_ip} vpn_client_ip: ${ifconfig_pool_remote_ip} datetime: $(date +'%Y/%m/%d %H:%M:%S') ${common_name} has disconnected from OpenVPN
      fi
      
      exit 0
      
      

      The only change in the original script are the lines with the curl-command.
      If i enter the url in the webbrowser or also in the shell of pfsense the message is sent. Sot the url is working. Just in the sript it seems not to work.

      Once it is working i will complete the message like this:
      /usr/local/bin/curl "https://api.telegram.org/bot-my-personal-api-key/sendMessage?chat_id=xxx&text=CONNECT user_name: ${common_name} remote_pub_address: ${trusted_ip} vpn_client_ip: ${ifconfig_pool_remote_ip} datetime: $(date +'%Y/%m/%d %H:%M:%S') ${common_name} has connected to OpenVPN"

      Does anyon have an idea what can be the problem?

      Thanks very much,
      Willy

      S 1 Reply Last reply Reply Quote 0
      • S
        ssppcc @willi25
        last edited by

        @willi25 Finally, did you get it fixed? I'm also interested in sending notifications via telegram

        1 Reply Last reply Reply Quote 0
        • B
          brrugg
          last edited by brrugg

          @ssppcc . Very old topic, I know.

          This changed script works for me.

          Of course you have to change the bot and chat details.

          Keep in mind that an update will overwrite the script for the original version.

          #!/bin/sh
          
          
          if [ "$script_type" = "client-connect" ]; then
          
                  /usr/local/bin/curl -s -X POST https://api.telegram.org/bot123456789:AAFfG1BrH5abcdefghijCAsFrAtbW8n0_A9/sendMessage -d chat_id=123456789 -d text="OpenVPN Server%0a$common_name connected from $untrusted_ip" 
          
          	if [ -f /tmp/$common_name ]; then
          		/bin/cat /tmp/$common_name > $1
          		/bin/rm /tmp/$common_name
          	fi
           
          elif [ "$script_type" = "client-disconnect" ]; then
          
                  /usr/local/bin/curl -s -X POST https://api.telegram.org/bot123456789:AAFfG1BrH5abcdefghijCAsFrAtbW8n0_A9/sendMessage -d chat_id=123456789 -d text="OpenVPN Server%0a$common_name disconnected from $untrusted_ip. Duration: $time_duration seconds" 
          
          	command="/sbin/pfctl -a 'openvpn/$common_name' -F rules"
          	eval $command
          	/sbin/pfctl -k $ifconfig_pool_remote_ip
          	/sbin/pfctl -K $ifconfig_pool_remote_ip
          fi
          
          exit 0
          
          
          p1terP 1 Reply Last reply Reply Quote 2
          • noplanN
            noplan
            last edited by

            send notification per mail
            see here ...

            https://forum.netgate.com/topic/151351/email-notification-openvpn-client-connect-common-name/26

            thanks for the telegram version !

            1 Reply Last reply Reply Quote 0
            • p1terP
              p1ter @brrugg
              last edited by p1ter

              @brrugg Thanks
              And for noobs like me don't forget :
              Web-GUI - OpenVpn - Advanced Configuration - add this

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

              make script executable

              chmod +x /root/openvpn-telegram.sh
              

              in my case

              P 1 Reply Last reply Reply Quote 1
              • P
                patrick.pesegodinski @p1ter
                last edited by

                @p1ter Does this command work in the 2.6.0 version?

                Because for me, with command entered in advanced, the credential not validated.

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post
                Copyright 2025 Rubicon Communications LLC (Netgate). All rights reserved.