Internet failover is essential to business health and resiliency. Failover prevents internet downtime that can cripple business operations. Automatic failover techniques automatically redirects internet/data in the event of a network failure, thus keeping end-users connected and working.
Mikrotik ROS v7 brings many changes including the scripting updates, breaking older scripting functionalities. Following is customized script for ISP connectivity test, which performs following functions,
- Ping remote destination IP (intenret host or ISP Gateway) using ARP , Source IP & Interface,
(This is useful when ISP have provided you P2P IP on your mikrotik, & this result no internet on mikrotik itself, so we can use ISP provided public ip as source NAT to faciliate ping results) - Add timestamp into global variable,
- If PING fails to remote destination, perform required actions like change route destination or whatever you like,, or send email
- Set different variables
- After failure, if ping response comes OK , then perform actions like reverting the routes, or send email
Script: Tested with ROS 7.15.x
(I have scheduled it to run every 10 seconds, & based on threshold count, it will perform action)
# Mikrotik Failover Script for ROS 7.x # This script is also useful if your ISP has given you P2P IP, so Mikrotik itself cannot ping any internet destination # 1 July 2024 # aacable.wordpress.com / aacable AT Hotmail DOT com # Define different Variables :global ISP1LastChkTime ([ / system clock get date ] . " " . [ / system clock get time ]); :global ISP1name :global ISP1Health :set ISP1name WAN1-MYISP :local ISP1name WAN1-MYISP # ISP-1 Interface :local ISP1INT WAN1 # ISP-1 Default Gateway :local ISP1GW 103.163.254.93 # ISP-1 NATTED IP, (IP that ISP have provided for natted traffic, since we have p2p ip on Mikrotik therefore we have to use NATTED ip in order # to facilitate Mikrotik ping for internet :local ISP1NatIp 103.163.254.94 # IP which Mikrotik will test for internet connectivity status, you can set it to any :local ISP1PingTarget 8.8.8.8 :local ISP1PingCount 2 #:log warning "TESTING $ISP1GW via $ISP1INT GW $ISP1GW using $ISP1NatIp Mask IP ..." # Please fill how many ping failures are allowed before fail-over happens :local ISP1FailTreshold 2 # Define the distance increase of a route when it fails :local ISP1DistanceIncrease 2 # -------------- stop editing here -------------- # Declare the global variables :global PingFailCountISP1 # This initializes the PingFailCount variables, in case this is the 1st time the script has ran :if ([:typeof $PingFailCountISP1] = "nothing") do={:set PingFailCountISP1 0} # This variable will be used to keep results of individual ping attempts :local ISP1PingResult # Check ISP1 :set ISP1PingResult [ping $ISP1GW arp-ping=yes count=$ISP1PingCount interface=$ISP1INT] :put $ISP1PingResult #:log warning $ISP1PingResult # If ping fails , result will be 0, so proceed with fail action :if ($ISP1PingResult = 0) do={ :if ($PingFailCountISP1 < ($ISP1FailTreshold+2)) do={ :set PingFailCountISP1 ($PingFailCountISP1 + 1) :if ($PingFailCountISP1 = $ISP1FailTreshold) do={ :log error "$ISP1name has a problem en route to $ISP1PingTarget - Performing Required Actions ..." :log warning "Disabling $ISP1name INTERFACE $ISP1INT ... 10 SECONDS DELAY" /ip route dis [find comment="WAN1_USERS_ROUTING_VIA_WAN1"] /ip route set distance=10 [find comment="WAN1"] /interface/disable [find comment="$ISP1name"] :delay 3 :log warning "Enabling $ISP1name INTERFACE $ISP1INT ..." /interface/enable [find comment="$ISP1name"] :log warning "Done." } } } :if ($ISP1PingResult = 2) do={ :if ($PingFailCountISP1 > 0) do={ :set PingFailCountISP1 ($PingFailCountISP1 - 1) :if ($PingFailCountISP1 = ($ISP1FailTreshold -1)) do={ :log warning "$ISP1name can reach $ISP1PingTarget again - Performing Required Actions ..." /ip route ena [find comment="WAN1_USERS_ROUTING_VIA_WAN1"] /ip route set distance=1 [find comment="WAN1"] :log warning "Done." } } } :if ($PingFailCountISP1 = 0) do={ :set $ISP1Health UP } else={ :set $ISP1Health DOWN }
When Al OK (Below SS)
When ISP failure occurs… (below SS)
Logs when ISP failure occurs (Below SS)
Logs when ISP recovers (Below SS)
- Older Scripts for Reference purposes…
Regard’s
Syed Jahanzaib