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" ]
}

Watch immediate gateway and reset the dhcp client

This will watch the immediate gateway and bounce the dhcp client if it's no longer reachable. This is useful if your client radio is external to the device you're doing wan fail over on. This helps when switching between cell hotspots or when switching back

# tag which is in the comment of the routes installed
local RTAG "B";
# Distance to set on the def route
local RDIST 1;
# address to test health of the route
local TADDR "8.8.4.4";
# get the dhcp client definition name by the interface
local clientName [ /ip/dhcp-client/get [ find interface=ether5 ] name ];

: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" ]
  /tool/netwatch/remove [ find comment="NWG $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
  /tool/netwatch/add comment="NWG $RTAG" host=$"gateway-address" interval=5s down-script="{:log info \"NWG gateway dead, restarting dhcp client\"; /ip/dhcp-client/disable $clientName; /ip/dhcp-client/enable $clientName}"
} else={
  :log info "*** DHCP unbound"
  /ip route remove [ find comment="DEF $RTAG" ]
  /ip route remove [ find comment="REC $RTAG" ]
  /tool/netwatch/remove [ find comment="NWG $RTAG" ]
}