Code: Select all
{
:log info "DHCP client being executed for backup internet link"
# Find route by matching comment, and count number of times route exists
:local count [/ip route print count-only where comment="HOST-ON-WAN-BACKUP"]
# If just got an IP address
:if ($bound=1) do={
# If no such route, just add a new one
:if ($count = 0) do={
/ip route add gateway=$"gateway-address" comment="HOST-ON-WAN-BACKUP"
}
# Else at least one such route exists
else={
# If found exactly one match, update it
:if ($count = 1) do={
:local test [/ip route find where comment="HOST-ON-WAN-BACKUP"]
:if ([/ip route get $test gateway] != $"gateway-address") do={
/ip route set $test gateway=$"gateway-address"
}
# Else multiple routes found, unable to update
} else={
:log error "DHCP Client Script: Multiple routes found for HOST-ON-WAN-BACKUP"
}
}
}
# Else just lost the IP address
else={
/ip route remove [find comment="HOST-ON-WAN-BACKUP"]
}
}