I’ve wrote a script that detects when Pi-Hole is no longer working, and automatically switches to public DNS 1.1.1.2,1.0.0.2.
Disclaimer: I am aware of possibility to set multiple DNS servers, but for Pi-Hole to work you need to set only Pi-Hole IP address.
Use case: Set-up Mikrotik and RPI with Pi-Hole. When RPI goes down, internet will “stop” working for everyone on the LAN, and you don’t want it to happen. Add script to Mikrotik that detects when Pi-Hole is no longer resolving queries and switch all DNS settings on Mikrotik to public DNS servers, such as 1.1.1.2,1.0.0.2. Also notify yourself that RPI is down. Second RPI costs money, and brings no benefits rather than high-availability, so this is not an option in this case.
Variant 1 - changes only DNS server for router. Assumes that all devices use router as the only DNS server:
:local currentDNS [/ip dns get server]
:local piholeDNS "192.168.0.50"
:local backupDNS "1.1.1.2,1.0.0.2"
:local testDomain "www.google.com"
:if ($currentDNS = $piholeDNS) do={
:do {
:resolve $testDomain server $piholeDNS
} on-error={
/ip dns set servers=$backupDNS
}
} else={
:do {
:resolve $testDomain server $piholeDNS
/ip dns set servers=$piholeDNS
} on-error={}
}
Variant 2 - changes only DNS server for router + notify yourself when Pi-Hole goes down via Telegram Bot. Assumes that all devices use router as the only DNS server:
:local telegramBotKey "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
:local chatID "XXXXXXXXX"
:local currentDNS [/ip dns get server]
:local piholeDNS "192.168.0.50"
:local backupDNS "1.1.1.2,1.0.0.2"
:local testDomain "www.google.com"
:if ($currentDNS = $piholeDNS) do={
:do {
:resolve $testDomain server $piholeDNS
} on-error={
/ip dns set servers=$backupDNS
/tool fetch "https://api.telegram.org/bot$telegramBotKey/sendmessage?chat_id=$chatID&text=Pi-Hole not working! Changed DNS from $currentDNS to $backupDNS." keep-result=no
}
} else={
:do {
:resolve $testDomain server $piholeDNS
/ip dns set servers=$piholeDNS
/tool fetch "https://api.telegram.org/bot$telegramBotKey/sendmessage?chat_id=$chatID&text=Pi-Hole is working again. Changed DNS from $currentDNS to $piholeDNS." keep-result=no
} on-error={}
}
Variant 3 - changes DNS server for router and for all the networks (IP → DHCP Server → Networks). Depending on lease time, DNS will not update instantly for all clients, but allows Pi-Hole to show what uses it:
:local currentDNS [/ip dns get server]
:local piholeDNS "192.168.0.50"
:local backupDNS "1.1.1.2,1.0.0.2"
:local testDomain "www.google.com"
:if ($currentDNS = $piholeDNS) do={
:do {
:resolve $testDomain server $piholeDNS
} on-error={
/ip dns set servers=$backupDNS
/ip dhcp-server network set [find] dns-server=$backupDNS;
}
} else={
:do {
:resolve $testDomain server $piholeDNS
/ip dns set servers=$piholeDNS
/ip dhcp-server network set [find] dns-server=$piholeDNS;
} on-error={}
}
Variant 4 - changes DNS server for router and for all the networks (IP → DHCP Server → Networks) + notify yourself when Pi-Hole goes down via Telegram Bot. Depending on lease time, DNS will not update instantly for all clients, but allows Pi-Hole to show what uses it:
:local telegramBotKey "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
:local chatID "XXXXXXXXX"
:local currentDNS [/ip dns get server]
:local piholeDNS "192.168.0.50"
:local backupDNS "1.1.1.2,1.0.0.2"
:local testDomain "www.google.com"
:if ($currentDNS = $piholeDNS) do={
:do {
:resolve $testDomain server $piholeDNS
} on-error={
/ip dns set servers=$backupDNS
/ip dhcp-server network set [find] dns-server=$backupDNS;
/tool fetch "https://api.telegram.org/bot$telegramBotKey/sendmessage?chat_id=$chatID&text=Pi-Hole not working! Changed DNS from $currentDNS to $backupDNS." keep-result=no
}
} else={
:do {
:resolve $testDomain server $piholeDNS
/ip dns set servers=$piholeDNS
/ip dhcp-server network set [find] dns-server=$piholeDNS;
/tool fetch "https://api.telegram.org/bot$telegramBotKey/sendmessage?chat_id=$chatID&text=Pi-Hole is working again. Changed DNS from $currentDNS to $piholeDNS." keep-result=no
} on-error={}
}
Usage: Use system → scheduler → add. Set interval to 00:00:30, any name and paste script into “On Event:” field. Do not forget to change variable values to match your Pi-Hole IP address.
EDIT: Updated scripts according to suggestions in comments & offered more variants.