According to my presentation in the 2018 MUM conference in Thailand, I presented in the topic “Monitoring the Internet Connections of WAN Links
with Only Routing Configuration”. https://mum.mikrotik.com/presentations/TH18/presentation_5725_1534743837.pdf
After the conference, I was asked from many people in Thailand on how to automatically update the ISP gateway addresses at routes if the ISP gateway addresses at the WAN interfaces were changed. My presentation showed them how to configure the recursive routes (i.e. routes with the static ISP gateways). As they asked, I realized that some ISPs sometimes change their gateway addresses. I have responded them with a solution to use a script that is able to automatically update the gateway addresses of the routes. I would like to share the following script here. Hopefully it may be useful for others.
In case of PPPoE Client at a WAN Link, the script is as follows:
:local newgw [/ip address get [find interface="pppoe-out1"] network];
:local routegw [/ip route get [find comment="isp1"] gateway ];
:if ($newgw != $routegw) do={
/ip route set [find comment="isp1"] gateway=$newgw;
}
Note that you can run it on the On Up event of PPPoE client by putting it in the PPPoE Client’s profile.
In case of DHCP Client at a WAN Link, the script is as follows:
:local newgw [ip dhcp-client get [find interface="ether1"] gateway];
:local routegw [/ip route get [find comment="isp1"] gateway ];
:if ($newgw != $routegw) do={
/ip route set [find comment="isp1"] gateway=$newgw;
}
Note that you can run it on the system scheduler with an appropriate period of time.
If you want to apply for more than one WAN links, for example, two WAN links (PPPoE Clients), just copy the above script and paste like this.
:local newgw1 [/ip address get [find interface="pppoe-out1"] network];
:local routegw1 [/ip route get [find comment="isp1"] gateway ];
:if ($newgw1 != $routegw1) do={
/ip route set [find comment="isp1"] gateway=$newgw1;
}
:local newgw2 [/ip address get [find interface="pppoe-out2"] network];
:local routegw2 [/ip route get [find comment="isp2"] gateway ];
:if ($newgw2 != $routegw2) do={
/ip route set [find comment="isp2"] gateway=$newgw2;
}
Hello my friend
I have internet from an isp.My isp has made settings that only the modem can connect and no bridge mode is used.
For some reason I have to use this isp.I connect modem to Mikrotik router and active dhcp client and always receive ip with CIDR 24 and the gateway is always 100.0.0.1 but it is not reachable.
For example, ip is 100.75.15.121/24, I have to find the gateway manually, which for example goes up to 100.75.15.1
I am not very familiar with the script and my isp is not willing to cooperate.
The only idea I have is to get dhcp client network and add it to 1 and put it in the gateway.
please help me.
:local DHCPINFO [/ip dhcp-client find where comment="WAN2"]
:local DHCPGW [/ip dhcp-client get $DHCPINFO gateway]
:foreach managedRoute in=[/ip route find where comment~"DHCP"] do={
:local RouteGateway [/ip route get $managedRoute gateway]
:if ($RouteGateway != $DHCPGW) do= {
:ip route set $managedRoute gateway=$DHCPGW
:log info "Dynamic DHCP route changed - $RouteGateway > $DHCPGW"
}
}
This script will search for a dhcp client with “WAN2” in the comment section and update all routes which have “DCHP” in the comment.
I put it inside IP > DHCP Client > DHCP of assigned interface > Advanced, but you can also use it in system scripts with a schedule.