Community discussions

MikroTik App
 
xbmcgotham
just joined
Topic Author
Posts: 13
Joined: Thu Feb 13, 2020 3:00 pm

Completing a script for checking and updating dynamic ISPs Gateway

Wed Mar 17, 2021 12:23 pm

Hi,

Hope someone can assist as I am not very good with Mikrotik router scripts.

Environment:
* Mikrotik Router is behind a modem of a ISP provider. This provider only allows dynamic IPs so the IP address and Gateway regularly change and this breaks the internal gateway setup.
* The ISP modem is connected directly to MikroTik router port etherX.
* The connection is setup within MikroTik as a "DHCP Client <etherX>"
* The Mikrotik router is directly connected over a VPN tunnel to another mikrotik router at a data center with a fixed IP address. Through this data center the traffic from within the LAN is pushed in and out.

Goal:
1. Add a script to "DHCP Client <etherX>" that automatically checks every 10 min. the Gateway address provided for by the ISP provider.
2. If it changed, update the new Gateway address in the Routing table at the Route that has the data centers static IP address [xxx.xxx.xxx.xxx].
3. Clean up the data reference to the old gateway address [xxx.xxx.xxx.xxx unreachable etherX] in the Routing table.

Hope someone could assist me with this script.

Much appreciated.
Thanks
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Completing a script for checking and updating dynamic ISPs Gateway

Thu Mar 18, 2021 10:41 am

DHCP client should update all you need. But if the problem that new IP breakes the VPN, look at Miktotiks Cloud solution.

 /ip cloud print
          ddns-enabled: yes
  ddns-update-interval: none
           update-time: yes
        public-address: <shows you public ip>
              dns-name: <serial number of router as part of DNS name>.sn.mynetname.net
                status: updated
 
xbmcgotham
just joined
Topic Author
Posts: 13
Joined: Thu Feb 13, 2020 3:00 pm

Re: Completing a script for checking and updating dynamic ISPs Gateway

Thu Mar 18, 2021 1:45 pm

Hi @Jotne
Thanks for your response.

That is currently not happening unfortunately. We have to manual adjust the gateway in the routing table each time its changed by the ISP. Otherwise the connection keeps disconnected. I dont see any settings either in the DHCP Client ether9 panel that will allow for this automation. I hope you know of a script that can do this automation by checking the gateway and update it if its different in the DHCP Client ether9 configuration.

Not sure about the cloud option as all is nicely working for us except for this only issue. There must be some script available to do this simply, correct?

Kind regards,
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Completing a script for checking and updating dynamic ISPs Gateway

Thu Mar 18, 2021 3:36 pm

So you get a new IP address, but does not get a Default Gateway. Seems to me then that someone does not follow standard DHCP.
 
xbmcgotham
just joined
Topic Author
Posts: 13
Joined: Thu Feb 13, 2020 3:00 pm

Re: Completing a script for checking and updating dynamic ISPs Gateway

Thu Mar 18, 2021 4:09 pm

@Jotne

We have different routing tables running parallel. Depending on the route list rules and the src. address certain traffic goes over different ISP providers. This might prevent your "standard" DHCP refresh from working correctly. One of these ISPs is used to route the VPN tunnel to the other mikrotik router at a data center. Its this specific route I like to auto update its gateway for.

For this reason I am looking for a script to do this. If you have a script in mind that can check the gateway and overwrite a particular routs gateway, please share.

Or are we missing here something?

Thanks
 
2frogs
Forum Veteran
Forum Veteran
Posts: 713
Joined: Fri Dec 03, 2010 1:38 am

Re: Completing a script for checking and updating dynamic ISPs Gateway

Thu Mar 18, 2021 5:43 pm

Put this in the script under dhcp-client. It will update only if there is a changed.
:if ($bound=1) do={/ip route set [find dst-address=X.X.X.X/32] gateway=$"gateway-address" }
 
xbmcgotham
just joined
Topic Author
Posts: 13
Joined: Thu Feb 13, 2020 3:00 pm

Re: Completing a script for checking and updating dynamic ISPs Gateway

Thu Mar 18, 2021 5:53 pm

Awesome @2frogs
Thanks for sharing!

Could I ask. the X.X.X.X/32 part, how do I read this?

Does this say:
If bound with "ISP" is 0 (not bound) than perform the following: In IP route table find the route with address= [the IP address it had] and change its gateway address to the "new gateway address".

And how often does the MikroTik router read this script? Or does it trigger this script immediately upon changing of the gateway?

Thanks again!
 
2frogs
Forum Veteran
Forum Veteran
Posts: 713
Joined: Fri Dec 03, 2010 1:38 am

Re: Completing a script for checking and updating dynamic ISPs Gateway

Thu Mar 18, 2021 6:39 pm

If I understood your requirements, X.X.X.X/32 (example 111.222.112.221/32) is static IP of data center.

From: https://wiki.mikrotik.com/wiki/Manual:IP/DHCP_Client
bound - 1 - lease is added/changed; 0 - lease is removed
So this says when lease is added (new/reboot) or changed to set the gateway for the route that contains X.X.X.X/32 to the new gateway.

It only triggers when lease is added/changed.
 
xbmcgotham
just joined
Topic Author
Posts: 13
Joined: Thu Feb 13, 2020 3:00 pm

Re: Completing a script for checking and updating dynamic ISPs Gateway

Thu Mar 18, 2021 7:04 pm

Thanks so much @2frogs for the additional information.
I will give your script a try, hopefully that will solve it finally. :-)
 
xbmcgotham
just joined
Topic Author
Posts: 13
Joined: Thu Feb 13, 2020 3:00 pm

Re: Completing a script for checking and updating dynamic ISPs Gateway

Wed Mar 31, 2021 1:54 pm

Put this in the script under dhcp-client. It will update only if there is a changed.
:if ($bound=1) do={/ip route set [find dst-address=X.X.X.X/32] gateway=$"gateway-address" }
This script is working great, thanks again!
Just wondering if you could also have a look at the following and are able to suggest/write another script that will do the same for a second dynamic ISP I have but which doesn't have a dedicated "Dst. Address". In this case its 0.0.0.0/0 and other routs in the list have also the same 0.0.0.0/0, so its not possible to select the correct route in the list on just the Dst. Address.

I can only identify one way of selecting the correct route with a combination of the Routing Mark with the Dst. Address.

In my situation their are 2 routs listed in the list having the same Routing Mark. One with Dst. Address 0.0.0.0/0 [related to ether8] and the other with 192.168.0.0/24 [related to Bridge1]

If I could use a similar script that would look for changes of the gateway FOR the route that only has Routing Mark = "ISP2" AND Dst. Address "0.0.0.0/0".

Hope this is possible to achieve this way?

Thanks again for the help!
 
2frogs
Forum Veteran
Forum Veteran
Posts: 713
Joined: Fri Dec 03, 2010 1:38 am

Re: Completing a script for checking and updating dynamic ISPs Gateway

Wed Mar 31, 2021 4:28 pm

Just need to add "where" to the find and you can search using multiple parameters.
:if ($bound=1) do={/ip route set [find where dst-address=0.0.0.0/0 routing-mark=ISP2] gateway=$"gateway-address" }
Alternatively, you could set comment=ISP2-Default or something unique and use that instead.
 
xbmcgotham
just joined
Topic Author
Posts: 13
Joined: Thu Feb 13, 2020 3:00 pm

Re: Completing a script for checking and updating dynamic ISPs Gateway

Wed Mar 31, 2021 4:35 pm

Just need to add "where" to the find and you can search using multiple parameters.
:if ($bound=1) do={/ip route set [find where dst-address=0.0.0.0/0 routing-mark=ISP2] gateway=$"gateway-address" }
Alternatively, you could set comment=ISP2-Default or something unique and use that instead.
Thanks! Yes, that looks easy enough. thinking it out is more of an issue for me. :-)
I will try it out.
 
User avatar
krafg
Forum Guru
Forum Guru
Posts: 1020
Joined: Sun Jun 28, 2015 7:36 pm

Re: Completing a script for checking and updating dynamic ISPs Gateway

Thu Jun 10, 2021 6:08 pm

Put this in the script under dhcp-client. It will update only if there is a changed.
:if ($bound=1) do={/ip route set [find dst-address=X.X.X.X/32] gateway=$"gateway-address" }
Thanks a lot, I will try it with routing-mark option.

Regards.
 
ajgnet
newbie
Posts: 35
Joined: Wed Apr 27, 2022 1:57 am

Re: Completing a script for checking and updating dynamic ISPs Gateway

Wed Apr 27, 2022 1:14 pm

New to MikroTik and found the below script, added to each dhcp client, very helpful to get my 3 ISPs working:
:if ($bound=1) do={/ip route set [find where dst-address=0.0.0.0/0 routing-mark=ISP1] gateway=$"gateway-address" }
Just had two questions... first, is this still the best way to update the default route where a gateway's IP is assigned through DHCP? And second, is it necessary to add as second script that removes the gateway if the lease is removed (if ($bound=2)...)

Who is online

Users browsing this forum: No registered users and 24 guests