User Tools

Site Tools


networking:router:mikrotik_resolve

This is an old revision of the document!


Use Hostnames in Mikrotik Routers

Mikrotik routers, like most others, do not directly support the use of hostnames instead of IP addresses.

However, we can use a simple script to resolve a hostname to an address-list, even if the hostname points to multiple IP addresses.

If the :resolve command does not complete normally, the script aborts and you will be left with a bad address-list.
As written, this script will not resolve multiple hostnames.

Example Scripts

resolve_delivery.antispamcloud.com

:!: This script is intended to resolve multiple hostnames in the add line.

/system script
add comment="Resolve DNS Hostname to Address List" name=resolve_delivery.antispamcloud.com owner=admin \
    policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=":local hosts \
    {\"delivery.antispamcloud.com\"}\r\
    \n:foreach k,v in=\$hosts do={\r\
    \n  :log info \"Doing \$v\"\r\
    \n  :local listname \$v\r\
    \n  :resolve \$v\r\
    \n  :local iscname [/ip dns cache all find where name=\$v and type=\"CNAME\"]\r\
    \n  :if (\$iscname != \"\") do={\r\
    \n    :local newname [/ip dns cache all get \$iscname data]\r\
    \n    :log info \"\$v is CNAME to \$newname\"\r\
    \n    :set v \$newname\r\
    \n  }\r\
    \n  :resolve \$v\r\
    \n  /ip firewall address-list remove [/ip firewall address-list find where list=\$listname]\r\
    \n  :foreach i in=[/ip dns cache all find where name=\$v and type=\"A\"] do={\r\
    \n    :local ipaddr [/ip dns cache all get \$i data]\r\
    \n    /ip firewall address-list add list=\$listname address=\$ipaddr comment=\$v\r\
    \n    :log info \"IP address: \$ipaddr\"\r\
    \n  }\r\
    \n  /ipv6 firewall address-list remove [/ipv6 firewall address-list find where list=\$listname]\r\
    \n  :foreach i in=[/ip dns cache all find where name=\$v and type=\"AAAA\"] do={\r\
    \n    :local ipaddr [/ip dns cache all get \$i data]\r\
    \n    /ipv6 firewall address-list add list=\$listname address=\$ipaddr comment=\$v\r\
    \n    :log info \"IPv6 address: \$ipaddr\"\r\
    \n  }\r\
    \n}\r\
    \n:log info \"end\""
networking/router/mikrotik_resolve.1512492816.txt.gz · Last modified: 2017/12/05 09:53 by gcooper