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

    Email Notification - OpenVPN Client Connect (Common Name)

    Scheduled Pinned Locked Moved OpenVPN
    138 Posts 20 Posters 42.2k Views
    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.
    • provelsP
      provels @Gertjan
      last edited by provels

      @Gertjan said in Email Notification - OpenVPN Client Connect (Common Name):

      chmod +x /root/notify.sh
      chmod +x /root/disconnect.sh

      That was it. Student Driver here. Sorry. Works a treat! Thanks to both of you. Glad I read this post!
      Spock.jpg

      Peder

      MAIN - pfSense+ 24.11-RELEASE - Adlink MXE-5401, i7, 16 GB RAM, 64 GB SSD. 500 GB HDD for SyslogNG
      BACKUP - pfSense+ 23.01-RELEASE - Hyper-V Virtual Machine, Gen 1, 2 v-CPUs, 3 GB RAM, 8GB VHDX (Dynamic)

      1 Reply Last reply Reply Quote 0
      • ArmstrongA
        Armstrong @Gertjan
        last edited by

        @Gertjan Ah yeah, I should've mentioned that it needs to be executable too.

        I decided to go full on and just implement over the wire. Scripts work fine.

        The only thing I would ask, is that the "disconnect" part doesn't seem to send the disconnect notification until I reconnect? I disconnect, no email, then when I re-connect after a short period I receive two notifications:

        13:08:17 user_name: username vpn_client_ip: x.x.x.x on 18 March during : 120 seconds DISCONNECTED
        13:08:17 user_name: username vpn_client_ip x.x.x.x 18 March

        It doesn't really bother me, I only wanted to know the users, but just out of curiosity.

        Thank you for your help!

        provelsP 1 Reply Last reply Reply Quote 0
        • provelsP
          provels @Armstrong
          last edited by provels

          @Armstrong My disco msg lagged a little, but did come about a minute after. Your messages may have overlapped.

          EDIT - 2 minutes

          Peder

          MAIN - pfSense+ 24.11-RELEASE - Adlink MXE-5401, i7, 16 GB RAM, 64 GB SSD. 500 GB HDD for SyslogNG
          BACKUP - pfSense+ 23.01-RELEASE - Hyper-V Virtual Machine, Gen 1, 2 v-CPUs, 3 GB RAM, 8GB VHDX (Dynamic)

          ArmstrongA 1 Reply Last reply Reply Quote 0
          • ArmstrongA
            Armstrong @provels
            last edited by

            @provels Yup just tested that (with a little patience), it does indeed send through after about a minute. Must just take a bit for it to fully "close" the connection.

            1 Reply Last reply Reply Quote 0
            • ArmstrongA
              Armstrong
              last edited by

              Also before I go, I made one change (connecting IP) so adding it to this post if anyone wants that too and would like to just copy and paste.

              #!/usr/local/bin/php -q
              <?php
              	require_once("/etc/inc/notices.inc");
              	$local_connect_value = " user_name: " . getenv('common_name') . " vpn_client_ip: " . getenv('ifconfig_pool_remote_ip') . " from: " . getenv('trusted_ip') . " on " . date('F j, Y, g:i a');
              	if ( strrchr (__FILE__ , 'disconnect') ) {
              	$local_connect_value .= ", duration : " . getenv('time_duration') . " seconds, received : " . getenv('bytes_received') . " bytes, send : " . getenv('bytes_sent') ." bytes. DISCONNECTED.";
              	}
              	notify_all_remote($local_connect_value);
              ?>
              

              Thanks for the input everyone!

              provelsP 1 Reply Last reply Reply Quote 1
              • provelsP
                provels @Armstrong
                last edited by

                @Armstrong Great to have if someone happens to snag your creds. Thanks.

                Peder

                MAIN - pfSense+ 24.11-RELEASE - Adlink MXE-5401, i7, 16 GB RAM, 64 GB SSD. 500 GB HDD for SyslogNG
                BACKUP - pfSense+ 23.01-RELEASE - Hyper-V Virtual Machine, Gen 1, 2 v-CPUs, 3 GB RAM, 8GB VHDX (Dynamic)

                1 Reply Last reply Reply Quote 0
                • M
                  mikekoke
                  last edited by mikekoke

                  Hi, I followed the comments and set up the @Armstrong script but I'm not getting any notification.
                  I created the files, gave the permissions, added the two lines to openvpn and restarted the service.

                  ArmstrongA 1 Reply Last reply Reply Quote 0
                  • ArmstrongA
                    Armstrong @mikekoke
                    last edited by

                    @mikekoke Dumb question, do you have notifications set up in Pfsense to send you emails?

                    1 Reply Last reply Reply Quote 0
                    • M
                      mikekoke
                      last edited by

                      Yes of course, I also took the test and received the notification.

                      ArmstrongA 1 Reply Last reply Reply Quote 0
                      • ArmstrongA
                        Armstrong @mikekoke
                        last edited by

                        @mikekoke Gotta ask.

                        You can connect to the VPN and disconnect as well, no errors with syntax or anything? It should work right away (latest OpenVPN version) so I can only suggest some other issue in your configuration.

                        I would try my original script and config (client-connect /root/notify.sh) and see if that works for you firstly. If it doesn't then there's more amiss than I could tell you.

                        1 Reply Last reply Reply Quote 0
                        • M
                          mikekoke
                          last edited by

                          I can connect to the VPN and everything works correctly, the script I am using is the first one, that of your post.

                          ArmstrongA 1 Reply Last reply Reply Quote 0
                          • ArmstrongA
                            Armstrong @mikekoke
                            last edited by

                            @mikekoke Can you run that notify script from the terminal verbose and see what happens?

                            It sounds like there's a problem with notifications, if everything's running but you're not getting an email. As I said it works straight away if you have email notifications on and everything on the latest version.

                            1 Reply Last reply Reply Quote 0
                            • M
                              mikekoke
                              last edited by mikekoke

                              When I try to run it it exits written: Exception: Zend Extension ./disconnect.sh does not exist

                              edit: Problem solved. Thanks for your help.

                              ArmstrongA 1 Reply Last reply Reply Quote 0
                              • ArmstrongA
                                Armstrong @mikekoke
                                last edited by

                                @mikekoke What was the issue?

                                1 Reply Last reply Reply Quote 0
                                • M
                                  mikekoke
                                  last edited by

                                  I had to create the files from the terminal.

                                  1 Reply Last reply Reply Quote 1
                                  • noplanN
                                    noplan
                                    last edited by

                                    @Armstrong said in Email Notification - OpenVPN Client Connect (Common Name):

                                    i felt free to make some cahnges for a readable output.
                                    the output from disconnect.sh received via e-mail looks now like this.

                                    d74e201a-8482-4d3a-a70a-b7bd1605fc2e-grafik.png

                                    as far as i'm concerned with this output i don't need the connect.sh cuz as the more users there are
                                    the more e-mails you'll receive.

                                    the source for disconnect.sh is here.

                                    #!/usr/local/bin/php -q
                                    <?php
                                    	require_once("/etc/inc/notices.inc");
                                    	$local_connect_value = " \n user_name: " . getenv('common_name') . " \n vpn_client_ip: " . getenv('ifconfig_pool_remote_ip') ." connected from " . getenv('trusted_ip') . " on " . date('F j, Y, g:i a');
                                    	if ( strrchr (__FILE__ , 'disconnect') ) {
                                    	$local_connect_value .= ", \n duration : " . round(((getenv('time_duration'))/3600),2) . "  hours, or " . round(((getenv('time_duration'))/60),2) . "  minutes, or " . getenv('time_duration') . "  seconds,\n upload from vpn-client (received) : " . round(((getenv('bytes_received'))/1048576),2) . " MB, \n download to vpn-client (send) : " . round(((getenv('bytes_sent'))/1048576),2) ." MB. \n DISCONNECTED.";
                                    	}
                                    	notify_all_remote($local_connect_value);
                                    ?>
                                    
                                    

                                    to reduce the mass on mails i was thinkin to use disconnect.sh with (client specific overrides)
                                    e84382c6-c801-44a6-8e90-df09b927a4ab-grafik.png

                                    but it was not workin and ended with someting like this:
                                    Options error: option 'client-disconnect' cannot be used in this context

                                    The main reason i wanted to do this, i put my clients in categries like

                                    • management (trusted)
                                    • sysops (trusted)
                                    • employees (trusted ;)
                                    • contractors (notification needed)

                                    so that notification will only be sent if (untrusted or notification needed) as i mentioned earlier reduce tons of mails.
                                    but i'll open a topic on this one and edit this post later on.

                                    any hints are welcome
                                    #stayHealthy

                                    ArmstrongA 1 Reply Last reply Reply Quote 1
                                    • ArmstrongA
                                      Armstrong @noplan
                                      last edited by

                                      @noplan That definitely looks a lot better format-wise, thanks for the update. PHP isn't my strongest area.

                                      I only have a small number of users so it's OK with me to have the number of emails I do, though for a larger organization it makes sense. I'm afraid I couldn't comment on the use of client overrides, but someone else may be able to provide advice.

                                      1 Reply Last reply Reply Quote 0
                                      • noplanN
                                        noplan
                                        last edited by

                                        i'm so happy that you came up with a version i can play on.
                                        and look its workin. pretty fine.

                                        if someone digs into it a cool thing would be a email
                                        and a weekly report send by cron
                                        vpn-client | date | time-in | time-out | client-vpn-ip | client foreign ip | duration connection (hours) | received (MB) | send (MB) |

                                        mybe a nice project.
                                        so a weekly report of VPN-clients "timesheets" could be generated

                                        but maybe all things niceToHave on a firewall ;)

                                        writtin this post
                                        i am thinking to write the informations vom disconnect.sh into a txt file and send it via cron to
                                        another machine doing the formating and generating a sweet pdf. (this could be fun i think )

                                        #stayHealthy

                                        1 Reply Last reply Reply Quote 0
                                        • V
                                          vettalex
                                          last edited by

                                          Hi guys, sorry if I reopen this discussion;

                                          • I have configured the pfsense notifications and by sending the smtp test, the e-mail will arrive safely.
                                          • I created the notify.sh file and placed it under root
                                          • I ran the command: chmod + x /root/notify.sh to make it executable
                                          • I also ran the ls -al notify.sh command and the result is this:
                                            notify.sh ls -al.JPG
                                          • but I don't receive any email when I connect remotely, nothing at all :(
                                            The notify.sh file contains this:
                                            notify.sh.JPG
                                            I tried to manually start the notify.sh file with the ./notify.sh command under root, but nothing happens ...
                                            at this point I would like to check the notification logs and together with you understand the problem. Do you know where the notification logs are located?
                                          1 Reply Last reply Reply Quote 0
                                          • M
                                            mikekoke
                                            last edited by

                                            Create the notify.sh file from the pfsense gui with Diagnostics -> Edit File or from the terminal, I had the same error, I had created the file on windows and moved it to pfsense through winscp but it didn't work.

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