Community discussions

MikroTik App
 
rjwilson01
just joined
Topic Author
Posts: 1
Joined: Mon Jan 30, 2023 9:58 am

Script to add/remove routes based on ping results

Mon Jan 30, 2023 10:27 am

I've seen a few questions which relate to this - but no answers , all of the respnses which liked ot Wiki Articles liked to no longer present wiki entries eg viewtopic.php?p=602282

sO I'm going to ask if there is any examples actually present and available
I have a Hex (no WiFi , 5 ports )RouterOS v6.49.7 (stable)

What I need to do is implement a Cisoc SLA type functionalllity I have an interface to a Starlink router DHCP set on Ether1 - I want to ping the "far" end and if it is up add the route otuerhwise delete the route -

The following is sort of psedo code so ignore syntax errors

rem First see if the interface is working and can get to the internet - should fail instantly if interface is down otherwise if starlink is not working should get timeouts
rem TODO probably need to select some starlink specific address first and if they fail just about quickly eg 192.168.100.1 which is the disk - sopme CGNAT 10.x.x..x address which is the downlinkg station if I can work it out

:set rc1 ([/ping 8.8.8.8 interface=ether1 count=2] )
:set rc2 ([/ping 2.2.2.4 interface=ether1 count=2] )
set $RC3 0
:if (($rc1 >0) ||($rc2 >0 ) do :set $RC3 1
rem Now iif we can get out add the route to the starlink router which is 192.168.1.1 - and is not changable
:if (($rc1 =1) do /ip route add dst-address=0.0.0.0/0 gateway=192.168.1.1
:if (($rc1 =0) do /ip route remove dst-address=0.0.0.0/0 gateway=192.168.1.1

It seems fairly simple - I need to declare variables etc but does this make sense ? Am I not finding articles which address this?
I still need to find a scheduling example but the interface looks ok

I will be trying this out , but am quite busy so it will be some time - so I was hoping to find an example but did not
Does anyone have any pointers?
 
User avatar
anav
Forum Guru
Forum Guru
Posts: 18958
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

Re: Script to add/remove routes based on ping results

Tue Jan 31, 2023 11:28 pm

MT devices already have some functionality that may assist.
We can setup without too much fuss, a primary and failover such that the router switches to the next available WAN if not available and return to the primary when it comes back on line.

Pinging the far end is simply called recursive routing here....... you check a far IP such as 8.8.8.8 and if its pingable the ISP is up and also connected to the www.

Who is online

Users browsing this forum: rolling and 46 guests