Community discussions

MikroTik App
 
User avatar
ekarin
Trainer
Trainer
Topic Author
Posts: 34
Joined: Fri Jun 01, 2018 9:12 pm
Contact:

Script of automatically updating gateway address at routes

Mon Sep 24, 2018 6:01 pm

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/ ... 743837.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;
}
Last edited by ekarin on Sat Oct 17, 2020 11:47 am, edited 4 times in total.
 
adminken
just joined
Posts: 1
Joined: Tue Oct 02, 2018 9:24 pm

Re: Script of automatically updating gateway address at routes

Mon Nov 25, 2019 5:14 am

+1 sir.
 
Taymaz1987
just joined
Posts: 1
Joined: Tue Sep 01, 2020 9:41 am

Re: Script of automatically updating gateway address at routes

Tue Sep 01, 2020 10:18 am

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.
 
User avatar
FiiMitch
newbie
Posts: 35
Joined: Tue Aug 18, 2020 8:49 am
Location: Gold Coast, QLD
Contact:

Re: Script of automatically updating gateway address at routes

Tue Sep 01, 2020 12:31 pm

I've installed countless Mikrotik routers using VDSL modems for nothing more than modulation and hardware bridging.

Can you specify:

- The make and model of the modem you are using
- How you have configured the bridging
- How the ISP is terminating the connection i.e. PPPoE, IPoE, DHCP
- What Mikrotik device you are using and how it is connected/configured

Also what exactly is meant by:
ip is100.75.15.121/24, I have to find the gateway manually, which for example goes up to 100.75.15.1

Is this what you are currently receiving at the modem level from the ISP? Or what is being handed to your Mikrotik router from the modem?
 
volkirik
Member Candidate
Member Candidate
Posts: 208
Joined: Sat Jul 23, 2016 2:03 pm

Re: Script of automatically updating gateway address at routes

Fri Feb 12, 2021 5:59 pm

i also update srcnat rule.. thanks for posting gateway script
:local EthInterfaceName "ether1"
:local MyComment "terra"

:local newgw [/ip dhcp-client get [/ip dhcp-client find interface="$EthInterfaceName"] gateway];
:local routegw [/ip route get [/ip route find comment="$MyComment"] gateway ];
:if ($newgw != $routegw) do={
     /ip route set [/ip route find comment="$MyComment"] gateway=$newgw;
}

:local "assigned-addr" [/ip dhcp-client  get [/ip dhcp-client  find interface="$EthInterfaceName" ] address]

:if ( [ :len ($"assigned-addr") ] <= 0 ) do=\
{
  :log info "DHCPc update-srcnat: empty local address"
  :error "DHCPc update-srcnat: empty local address"
} else=\
{
  :log info "DHCPc update-srcnat: local address for $EthInterfaceName is ($"assigned-addr") (up)"
}

:local "local-address" [:pick $"assigned-addr" 0 ([:find $"assigned-addr" "/"])]

/ip firewall nat
:local srcnatPPPId
:set srcnatPPPId [ find chain=srcnat action=src-nat out-interface="$EthInterfaceName"]
#check if srcnat exists
:if ( [ :len $srcnatPPPId ] < 1 ) do=\
{
  /ip firewall nat add action=src-nat chain=srcnat log=yes out-interface="$EthInterfaceName" to-addresses=($"local-address")
  :log info "DHCPc update-srcnat: added srcnat for $EthInterfaceName to ($"local-address")"
} else=\
{
  :local oldip [/ip firewall nat get [/ip firewall nat find chain=srcnat action=src-nat out-interface="$EthInterfaceName"] to-addresses]
  :if ($"local-address" != $oldip) do={
    /ip firewall nat set [find chain=srcnat action=src-nat out-interface="$EthInterfaceName"] to-addresses=($"local-address")
    :log info "DHCPc update-srcnat: updated srcnat for $EthInterfaceName to ($"local-address")"
  }
}


/ 
 
cooling
just joined
Posts: 20
Joined: Tue Aug 22, 2006 12:59 pm

Re: Script of automatically updating gateway address at routes

Sat Jul 31, 2021 5:35 am

how to insert two pppoe gateway in one coment , example :
1. out-pppoe1 = gateway {192.168.1.1)
2. out- pppoe2 = gateway (10.10..1.1 ) all gateway dinamic

/ip route ( isp1 )
gateway 192.168.1.1, 10.10.1.1

script
:local newgw1 [/ip address get [find interface="pppoe-out1"] network];
:local newgw2 [/ip address get [find interface="pppoe-out2"] network];
:local routegw1 [/ip route get [find comment="Test"] gateway ];
:local routegw2 [/ip route get [find comment="Test"] gateway ];
:if ($newgw1 != $routegw1),($newgw2 != $routegw2) do={
/ip route set [find comment="Test"] gateway=$newgw1,$newgw2;
}

but can't work

thanks
 
volkirik
Member Candidate
Member Candidate
Posts: 208
Joined: Sat Jul 23, 2016 2:03 pm

Re: Script of automatically updating gateway address at routes

Sat Jul 31, 2021 11:48 am

:if ($newgw1 != $routegw1),($newgw2 != $routegw2) do={
https://wiki.mikrotik.com/wiki/Manual:S ... _Operators
:if (($newgw1 != $routegw1) or ($newgw2 != $routegw2)) do={
edit: brackets
Last edited by volkirik on Sun Aug 01, 2021 8:36 am, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script of automatically updating gateway address at routes

Sun Aug 01, 2021 5:34 am

:if ($newgw1 != $routegw1),($newgw2 != $routegw2) do={
https://wiki.mikrotik.com/wiki/Manual:S ... _Operators
:if ($newgw1 != $routegw1) or ($newgw2 != $routegw2) do={

Ignoring the other things...
And where do the brackets go?
:if (($newgw1 != $routegw1) or ($newgw2 != $routegw2)) do={
 
cooling
just joined
Posts: 20
Joined: Tue Aug 22, 2006 12:59 pm

Re: Script of automatically updating gateway address at routes

Sun Aug 01, 2021 7:38 am

:if ($newgw1 != $routegw1),($newgw2 != $routegw2) do={
https://wiki.mikrotik.com/wiki/Manual:S ... _Operators
:if ($newgw1 != $routegw1) or ($newgw2 != $routegw2) do={
i.m try but can't work
 
krixer
just joined
Posts: 1
Joined: Tue Jul 25, 2017 6:49 am

Re: Script of automatically updating gateway address at routes

Fri Nov 19, 2021 4:27 pm

Thank sir, hmm it works, but how about if same gateway?Image this example pic that the comment v4 is for ether4, but its reachable to ether5, because of the same gateway of different wan's.

just click this link if picture not view
https://postimg.cc/HJKjGCyG
 
maxpower
newbie
Posts: 26
Joined: Fri Dec 05, 2014 4:45 pm

Re: Script of automatically updating gateway address at routes

Mon Jan 31, 2022 7:49 pm

May be you should also do:
/ip firewall connection {:foreach r in=[find] do={remove $r}};
To reset NAT connections that were established using old gateway?
 
xgalsax1
just joined
Posts: 1
Joined: Tue Apr 12, 2022 3:50 am

Re: Script of automatically updating gateway address at routes

Tue Nov 15, 2022 11:01 pm

In my case I have a failover using this configuration https://help.mikrotik.com/docs/pages/vi ... d=26476608.
So I need to change multiple routes each time, here's the script I use:
: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.

Who is online

Users browsing this forum: diamuxin, Ellaham, qatar2022 and 25 guests