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 ”
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:
Add a script to “DHCP Client ” that automatically checks every 10 min. the Gateway address provided for by the ISP provider.
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].
Clean up the data reference to the old gateway address [xxx.xxx.xxx.xxx unreachable etherX] in the Routing table.
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
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?
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.
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?
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”.
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)…)