Stetting a dynamic gateway address to a static one

Hi

I’ve a static route set up to a PPPoE tunnel connecting to my ISP as the dynamic route to the ISP does not work/have the correct routing mark associated to it. This works fine until the PPPoE reconnects and the gateway changes.

I’d like to have a script check the dynamic route’s gateway and set my static one to match… so far I have this:

:global list “”; :foreach i in [/ip route find] do={:if ([:find [/ip route get $i gateway] “pppoe-out-adsl”]=0) do={:set list ($list . “,” . $i);}}; /ip route print detail without-paging from $list;

without digging into the details of MT scripting I’m a bit lost, I’m sure someone can help me out easily with this.

many thanks

bump

Are there just the two routes? Or more? Could you run this and post the results?

/ip route print detail

HI

There are lots of routes set up, here are some of the relevant ones:

 0   S  ;;; Match this route to the Dynamic ADSL default route
        dst-address=0.0.0.0/0 gateway=62.5.84.25 
        gateway-status=62.5.84.25 unreachable check-gateway=ping distance=1 
        scope=30 target-scope=10 routing-mark=vicADSL 

 1 X S  dst-address=10.6.1.0/24 gateway=10.6.10.254 
        gateway-status=10.6.10.254 inactive distance=1 scope=30 
        target-scope=10 routing-mark=vicADSL 

 2 X S  dst-address=10.6.11.0/24 gateway=10.6.10.254 
        gateway-status=10.6.10.254 inactive distance=1 scope=30 
        target-scope=10 routing-mark=vicADSL 

 3 A S  dst-address=192.168.2.0/24 pref-src=192.168.2.254 gateway=ether3 
        gateway-status=ether3 reachable distance=1 scope=10 target-scope=10 
        routing-mark=vicADSL 

 4 A S  ;;; RemoteSitesPPtP default route
        dst-address=0.0.0.0/0 gateway=172.20.100.254 
        gateway-status=172.20.100.254 reachable vlan10CorporateData 
        distance=1 scope=30 target-scope=10 routing-mark=RemoteSitesPPtP 

 5 A S  ;;; public guest access
        dst-address=0.0.0.0/0 gateway=214.196.252.44 
        gateway-status=214.196.252.44 reachable bridgehSoGuest distance=1 
        scope=30 target-scope=10 

 6  DS  dst-address=0.0.0.0/0 gateway=62.5.84.23 
        gateway-status=62.5.84.23 reachable pppoe-out-adsl-zen distance=1 
        scope=30 target-scope=10 

 7 ADC  dst-address=62.5.84.23/32 pref-src=32.69.141.118 
        gateway=pppoe-out-adsl-zen 
        gateway-status=pppoe-out-adsl-zen reachable distance=0 scope=10

Route 0 (62.5.84.25) is the one that needs to be set to the gateway in 6 (62.5.84.23) which changes on reconnect

This might work, assuming you only have 1 dynamic route where the gateway-status contains the string “reachable pppoe-out-adsl-zen”. First, run this command to check. If it comes back with 1, you’re fine. If it comes back with 2 or more, the script will have to be revised:

:put [ :len [ /ip route find gateway-status~"reachable pppoe-out-adsl-zen" dynamic ] ];

If that checks out ok, then this script should work:

# tested on RouterOS v4.17
# for the route that contains this comment, do the following
:foreach i in [ /ip route find comment~"Match this route to the Dynamic ADSL" ] do={
# get the gateway of the dynamic adsl route
	:local adslGateway [ /ip route get [ find gateway-status~"reachable pppoe-out-adsl-zen" dynamic ] gateway ];
# if the adsl Gateway IP does not match the static gateway IP
	if ( $adslGateway != [ /ip route get $i gateway ] ) do={
# set the static gateway to match
		/ip route set $i gateway=$adslGateway;
	}
}

Thanks! that works perfectly on ROS 5.8 :smiley:

Broke when I upgraded to ROS 5.18 due to an extra word and space being added to the route detail output text

reachable via  pppoe-out-adsl-zen



6  DS  dst-address=0.0.0.0/0 gateway=62.3.84.21 gateway-status=62.3.84.21 reachable via  pppoe-out-adsl-zen distance=1 scope=30 target-scope=10

modified the script and we’re back up working again…