Skip to content

Mikrotik Notes

Mikrotik DHCP client recursive route install script

# on the DHCP client def, use this script in the advanced tab
:if ( $bound=1) do={
  :log info ("*** DHCP bound, got gateway " . $"gateway-address")
  /ip route remove [ find comment="DEF1" ]
  /ip route remove [ find comment="REC1" ]
  /ip route add dst-address=8.8.8.8/32 gateway=($"gateway-address") comment="REC1" scope=10 target-scope=10 distance=1
  /ip route add dst-address=0.0.0.0/0 gateway=8.8.8.8 comment="DEF1" check-gateway=ping distance=1 target-scope=11
} else={
  :log info "*** DHCP unbound"
  /ip route remove [ find comment="DEF1" ]
  /ip route remove [ find comment="REC1" ]
}

Better DHCP recursive route install script

You can copy this into the dhcp client def and just change the variables as necessary. Update the distance to set priority for different connections.

# on the DHCP client def, use this script in the advanced tab

# tag which is in the comment of the routes installed
local RTAG "A";
# Distance to set on the def route
local RDIST 1;
# address to test health of the route
local TADDR "8.8.8.8";

:if ( $bound=1) do={
  :log info ("*** DHCP bound, got gateway " . $"gateway-address")
  /ip route remove [ find comment="DEF $RTAG" ]
  /ip route remove [ find comment="REC $RTAG" ]
  /ip route add dst-address="$TADDR/32" gateway=($"gateway-address") comment="REC $RTAG" scope=10 target-scope=10 distance=1
  /ip route add dst-address=0.0.0.0/0 gateway=($"TADDR") comment="DEF $RTAG" check-gateway=ping distance=$RDIST target-scope=11
} else={
  :log info "*** DHCP unbound"
  /ip route remove [ find comment="DEF $RTAG" ]
  /ip route remove [ find comment="REC $RTAG" ]
}