OpenVPN Client – Automatic reconnection??? (Solution)

ragametal

Contributor
Joined
May 4, 2021
Messages
188
This is the solution for the thread OpenVPN Client – Automatic reconnection??? which has been marked as "Read-only". Please follow that link to get the entire background. Moderators of this forum are more than welcome to move this post as they see pertinent to avoid duplicate threads.

For the benefit of others that may be facing a similar challenge, the following is the solution that worked for me.

My environment is as follows:
10.0.0.0 – LAN network
10.0.0.1 - OpenVPN Server (PFSense router/firewall)
10.0.0.20 - Local TrueNAS (Main System and replication source)
10.40.0.0 - OpenVPN Tunnel
10.40.0.30- Remote TrueNAS connecting to LAN via OpenVPN Client (Replication target)

In the end my solution was to create a script and run it every hour via a cron job.

The script is installed in the remote TrueNAS server that connects to my LAN using the native OpenVPN Client service. It PING the main TrueNAS server in my LAN to check if the OpenVPN connection is active. If it is down, it restarts the OpenVPN Client in the remote TrueNAS machine which in turn restarts the OpenVPN connection.

The script also sends an email notification every time it restarts the OpenVPN connection and logs the results of every connection check. It is not too bad as there will only be 24 entries a day.

The following is the script that i wrote. If you want to try it, just adjust the target email and IP for the PING function in accordance to your enviromnet,

Code:
#!/bin/bash
# Script to restart the OpenVPN Client if the OpenVPN tunnel is down
#
# This script is designed to be run by a cronjob in the remote
# TrueNAS machine running OpenVPN Client.
#
# By: Ragametal 02-06-2023
#
##########################################################################
# Modify the variables below
#
# Specify the local IP of the TrueNAS located in the network where the
# OpenVPN Server is installed.
TN_local_IP="10.0.0.20"

#information for email notifications
EMAIL_SUBJECT="Remote TrueNAS - VPN Connection is down"
TO_EMAIL_ADDRESS="user@yourdomain.com"

#name and location of the log file
TMP_OUTPUT="/var/log/openvpn_client_status.log"
OUTPUT="/var/log/openvpn_script.log"
# end of variables

# start of script
# do not change script below this line
###########################################################################

#create a log file if one is not found.
if [ ! -f $TMP_OUTPUT ]; then
        touch $TMP_OUTPUT
fi

if [ ! -f $OUTPUT ]; then
        touch $OUTPUT
fi

# ping the local TrueNAS server inside the same LAN
# as the OpenVPN Server
ping -c30 -i3 $TN_local_IP

if [ $? -eq 0 ];
 then
    echo 'the tunnel is UP'
    echo $(date)' the tunnel is UP'    >> $OUTPUT
 else
    echo $(date)' the tunnel is DOWN'    >> $OUTPUT
    echo 'To: '$TO_EMAIL_ADDRESS            > $TMP_OUTPUT
    echo 'Subject: '$EMAIL_SUBJECT            >> $TMP_OUTPUT
    echo '--------------------------------------------------' >> $TMP_OUTPUT
    echo 'VPN down'                            >> $TMP_OUTPUT
    echo $(date)                            >> $TMP_OUTPUT
    echo ''                                 >> $TMP_OUTPUT
    echo 'Restarting OpenVPN Client...'        >> $TMP_OUTPUT
    echo '--------------------------------------------------' >> $TMP_OUTPUT
    echo ''                                 >> $TMP_OUTPUT

    # Restart the OpenVPN client service
    service openvpn_client restart             >> $TMP_OUTPUT
    wait
    
    #Send email notification that openVPN has been restarted
    sendmail -t -oi < $TMP_OUTPUT
fi

exit 0;
 
Top