was looking for a similar solution and came up with an own script. wanted to share it, in case someone can need it:
[/Codebox]system scheduler export :
/system scheduler
add interval=1m name=auto-add-static-routes on-event=Add-static-routes-from-DNS policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-date=dec/03/2018 start-time=21:47:34
system script: Name: Add-static-routes-from-DNS (dont forget to edit gateway and vrf-mark like you need it)
this script resolves given dns names and adds routes for resolved ips.
important: edit static route options to fit your network!
custom routes can also be defined.
skips adding routes if already added.
created 2018 by chil.at
declare variables
local dnsNames
local customRoutes
local ipAddress
local gateway
local type
local distance
local scope
local targetscope
local routingmark
define dns records
set dnsNames ($dnsNames , “upgrade.mikrotik.com”)
set dnsNames ($dnsNames , “www.ubnt.com”)
set dnsNames ($dnsNames , “security.debian.org”)
set dnsNames ($dnsNames , “security-cdn.debian.org”)
set dnsNames ($dnsNames , “ftp.us.debian.org”)
define custom static routes
cloudflare DNS
set customRoutes ($customRoutes , “1.1.1.1/32”)
google DNS
set customRoutes ($customRoutes , “8.8.8.8/32”)
define static route options
set gateway “1.2.3.4@main”
set type “unicast”
set distance 200
set scope 30
set targetscope 10
set routingmark “VRF-MANAGEMENT”
do not edit below here!
clear DNS cache and all AutoAdded routes (enable only temporarily to clear in case of problems)
#ip dns cache flush
#ip route remove [find comment~“AutoAdded”]
#log info “cleared DNS cache and all AutoAdded routes.”
set DNS cache Max TTL to 10 minutes
#ip dns set cache-max-ttl=“10m”
set static routes from defined customRoutes
foreach route in=$customRoutes do={
if ([ip route find dst-address=“$route” gateway=$gateway type=$type distance=$distance scope=$scope target-scope=$targetscope routing-mark=$routingmark] != “”) do {
log debug “route exists to $route, skipping.”
} else {
ip route add dst-address=$route gateway=$gateway type=$type distance=$distance scope=$scope target-scope=$targetscope routing-mark=$routingmark comment=“AutoAdded”;
log info (“route added to $route”);
};
}
set static routes from defined dnsNames
foreach dnsName in=$dnsNames do={
set ipAddress ([:resolve $dnsName])
if ([ip route find dst-address=“$ipAddress/32” gateway=$gateway type=$type distance=$distance scope=$scope target-scope=$targetscope routing-mark=$routingmark] != “”) do {
log debug “route exists to $ipAddress ($dnsName), skipping.”
} else {
ip route add dst-address=$ipAddress gateway=$gateway type=$type distance=$distance scope=$scope target-scope=$targetscope routing-mark=$routingmark comment=“AutoAdded for dnsName: $dnsName”;
log info (“route added to $ipAddress ($dnsName)”);
};
}