• Contact
  • About Me
  • Privacy Policy
  • Disclaimer
DefenceDev
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
    • Bash Scripts
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
  • Web Tools
No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
    • Bash Scripts
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
  • Web Tools
No Result
View All Result
DefenceDev
No Result
View All Result
ADVERTISEMENT
Home Network Tutorials MikroTik

MikroTik: OpenVPN Client Failover Script

neo by neo
August 7, 2025
in MikroTik
0
MikroTik: OpenVPN Client Failover Script

MikroTik: OpenVPN Client Failover Script

0
SHARES
76
VIEWS
Share on FacebookShare on Twitter
Table of Contents
  • OpenVPN Client Failover Script
    • Problem Overview
  • Solution for Failover – Tunnel Status Failover Script
    • How the Script Works
    • Script Code
    • Script Implementation
  • Conclusion

OpenVPN Client Failover Script

Failover mechanisms play a crucial role in maintaining continuous connectivity between remote sites. In small office environments, branch offices, and industrial deployments with IoT devices, OpenVPN provides secure tunnels for data exchange. However, when the VPN tunnel drops, these systems lose connectivity unless you act quickly.

This is where automated failover and notification mechanisms come in. With simple MikroTik scripting, you can detect OpenVPN tunnel failures, switch to a backup route or connection, and receive notifications the moment a problem appears.

Problem Overview

MikroTik does not offer built-in automatic failover or notification when an OpenVPN tunnel drops. This limitation becomes critical in environments where stable VPN connectivity ensures remote access, site-to-site operations, or IoT command channels. Without automation, downtime goes unnoticed until users or services begin to fail.

Solution for Failover – Tunnel Status Failover Script

MikroTik’s scripting engine gives you enough control to detect when an OpenVPN tunnel goes down and trigger a failover.

How the Script Works

The failover script automatically monitors the status of multiple OpenVPN client interfaces and ensures that at least one tunnel remains active. The logic follows a linear priority-based approach:

  1. Interface Definitions
    The script defines three OpenVPN clients in order of priority:
    • openvpn_client_1 – primary VPN tunnel
    • openvpn_client_2 – secondary (backup) VPN tunnel
    • openvpn_client_3 – tertiary (last-resort) VPN tunnel
      These names must match the actual interface names on your MikroTik router.
  2. Status Check
    The script loops through all OpenVPN client interfaces and checks which one is currently running (connected). It stores the name of the active tunnel in a variable.
  3. Failover Conditions
    • If openvpn_client_1 is enabled but not running, the script disables it and enables openvpn_client_2.
    • If openvpn_client_2 is enabled but not running, it disables openvpn_client_2 and enables openvpn_client_3.
    • If none of the tunnels are running (no active VPN client detected), the script reverts to openvpn_client_1 as the default and disables openvpn_client_3 to reset the state.
  4. Action Logging
    All tunnel switches are logged using the :log info command so that administrators can review failover events in the MikroTik system log.
  5. Scheduled Execution
    The script is intended to run at regular intervals (e.g., every 1 minute) using the system scheduler. This ensures real-time detection and correction of tunnel failures without manual intervention.

This logic ensures that the router always has an active route to the internet or remote location, even when the VPN fails.

Script Code

Save the script under the name vpn-failover-script.

# Define the OpenVPN client interface names
# IMPORTANT: Adjust these interface names to match your own configuration
:local openvpn_client_1 "openvpn_client_1"
:local openvpn_client_2 "openvpn_client_2"
:local openvpn_client_3 "openvpn_client_3"

# This variable will hold the name of the currently active OpenVPN client
:local activeClient ""

# Check all OpenVPN clients to find which one is currently running
:foreach i in=[/interface ovpn-client find] do={
    :local clientName [/interface ovpn-client get $i name]
    :local clientStatus [/interface ovpn-client get $i running]

    :if ($clientStatus = true) do={
        :set activeClient $clientName
    }
}

# Check if openvpn_client_1 is enabled but not running
:if ([/interface ovpn-client get [find name=$openvpn_client_1] disabled] = false && [/interface ovpn-client get [find name=$openvpn_client_1] running] = false) do={
    :log info ("openvpn_client_1 is enabled but not running. Disabling openvpn_client_1 and enabling openvpn_client_2.")
    /interface ovpn-client disable [find name=$openvpn_client_1]
    /interface ovpn-client enable [find name=$openvpn_client_2]

} else={

    # Check if openvpn_client_2 is enabled but not running
    :if ([/interface ovpn-client get [find name=$openvpn_client_2] disabled] = false && [/interface ovpn-client get [find name=$openvpn_client_2] running] = false) do={
        :log info ("openvpn_client_2 is enabled but not running. Disabling openvpn_client_2 and enabling openvpn_client_3.")
        /interface ovpn-client disable [find name=$openvpn_client_2]
        /interface ovpn-client enable [find name=$openvpn_client_3]

    } else={

        # If none are running, enable openvpn_client_1 as default
        :if ($activeClient = "") do={
            :log info "No active OpenVPN clients found. Enabling openvpn_client_1 and disabling openvpn_client_3."
            /interface ovpn-client disable [find name=$openvpn_client_3]
            /interface ovpn-client enable [find name=$openvpn_client_1]
        }
    }
}

Notes for the Readers

  • Adjustable Parameters: You must change the values of openvpn_client_1, openvpn_client_2, and openvpn_client_3 to match the names of your OpenVPN client interfaces. These are defined at the top of the script.
  • Failover Logic:
    • If client 1 fails (enabled but not running), switch to client 2.
    • If client 2 fails, switch to client 3.
    • If no clients are running, re-enable client 1 as a fallback.
  • Logging: The script logs each action for easier troubleshooting. You can view these logs under Log > Info in Winbox or WebFig.

Script Implementation

Step 1: Find OpenVPN Connections which will be Part of the Script

OpenVPN Clients List
OpenVPN Clients List

Step 2: Copy Script to Mikrotik Router

  1. Go to System > Scripts.
  2. Create a new script and paste the code.
  3. Script name: vpn-failover-script
Copy Script to MikroTik Router
Copy Script to MikroTik Router

Step 3: Create a Scheduler

  1. Open System > Scheduler, add a new entry:
    • Interval: 00:00:30
    • On Event: /system script run vpn-failober-script
  2. Make sure you test the VPN drop and failover route before putting it into production.
Create a Scheduler
Create a Scheduler

Conclusion

This failover script enhances the reliability of your MikroTik VPN setup by automatically reacting to tunnel failures. It ensures your remote sites or services stay connected, even when the primary VPN link drops. Combined with a monitoring or notification system (covered in the next post), it becomes a powerful high-availability solution.

ADVERTISEMENT

Combined with a proper OpenVPN setup and a reliable backend VPN server like SoftEther, this failover script ensures your remote access remains stable and automated.
If you haven’t set up your tunnel yet, follow this OpenVPN configuration guide for MikroTik.
For advanced setups, consider running a SoftEther VPN server as the remote endpoint.

ADVERTISEMENT

About The Author

neo

See author's posts

ADVERTISEMENT
Previous Post

GitHub: How to Add a Script or a Folder to Your Repository

Next Post

MikroTik OpenVPN: Sound Alert When VPN Tunnel Fails

neo

neo

Related Posts

MikroTik OpenVPN: Sound Alert When VPN Tunnel Fails
MikroTik

MikroTik OpenVPN: Sound Alert When VPN Tunnel Fails

MikroTik: Check Your Wireless Password
MikroTik

MikroTik: Check Your Wireless Password

MikroTik VLAN Configuration
MikroTik

MikroTik VLAN Configuration

MikroTik: Remote Access to Headquarter with OpenVPN
MikroTik

Configure OpenVPN Server on MikroTik Router for Remote Access

MikroTik: Configure SNMP for Network Monitoring
MikroTik

MikroTik: Configure SNMP for Network Monitoring

MikroTik: Port Forwarding (NAT) Configuration
MikroTik

MikroTik: Port 443 Forwarding to Web Server (NAT) Configuration

Next Post
MikroTik OpenVPN: Sound Alert When VPN Tunnel Fails

MikroTik OpenVPN: Sound Alert When VPN Tunnel Fails

Leave a Reply

Your email address will not be published. Required fields are marked *


Recommended

WordPress: Automatically Website Backup with Bash Script on Linux

WordPress: Automatically Website Backup with Bash Script on Linux

MikroTik: Port Forwarding (NAT) Configuration

MikroTik: Port 443 Forwarding to Web Server (NAT) Configuration

View & Copy Image Metadata Online – Camera & GPS Info

View & Copy Image Metadata Online – Camera & GPS Info

Online WebP to JPG Converter

Online WebP to JPG Converter

Image Metadata Remover – Clean EXIF and GPS Info Securely

Image Metadata Remover – Clean EXIF and GPS Info Online

Categories

  • Blog
  • Cloud
    • Private
  • Linux Tutorials
    • Bash Scripting Tutorials
    • Commands
    • Networking
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Solutions
    • Docker
  • Video Tutorials
    • MikroTik Videos
  • Web Tools
ADVERTISEMENT

DefenceDev Tutorials

defencedev Logo

Whether you’re just starting or looking to expand your skills, I hope you find useful information and engaging discussions here. Let me take you through my journey and the goals behind this space!

Follow Us

Recent News

View & Copy Image Metadata Online – Camera & GPS Info

View & Copy Image Metadata Online – Camera & GPS Info

Online WebP to JPG Converter

Online WebP to JPG Converter

  • Site Map
  • Privacy Policy
  • Facebook Page
  • Disclaimer
  • Contact
  • About Me

© 2025 defencedev.com - All rights reserved.

No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
    • Bash Scripts
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
  • Web Tools

© 2025 defencedev.com - All rights reserved.