I use this to check a DNS server and change mikrotik’s DNS server if it fails
:local PrimaryDNS "1.2.3.4";
:local BackupDNS "9.9.9.9,149.112.112.112";
:local TestDomain "google.com";
:local ConfiguredDNS [/ip dns get servers];
:if ($ConfiguredDNS = $PrimaryDNS) do={
:do {:put [:resolve $TestDomain server $PrimaryDNS];} on-error={log warning "Primary DNS $PrimaryDNS query failed. Setting $BackupDNS as DNS server";/ip dns set servers=$BackupDNS;}
} else={
:do {:put [:resolve $TestDomain server $PrimaryDNS]; log warning "Primary DNS $PrimaryDNS is back online. Setting $PrimaryDNS as DNS server";/ip dns set servers=$PrimaryDNS;} on-error={ }
}
You can change it to suit your needs.
Change PrimaryDNS with your DNS server.