Community discussions

MikroTik App
 
erkexzcx
just joined
Topic Author
Posts: 15
Joined: Mon Oct 07, 2019 11:42 pm

[Script] Automatically change DNS if Pi-hole is no longer working

Sun May 31, 2020 2:44 pm

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.
Last edited by erkexzcx on Sun May 31, 2020 7:43 pm, edited 1 time in total.
 
User avatar
jvanhambelgium
Member Candidate
Member Candidate
Posts: 295
Joined: Thu Jul 14, 2016 9:29 pm
Location: Belgium

Re: [Script] Automatically change DNS if Pi-hole is no longer working

Sun May 31, 2020 3:13 pm

This will not work for all clients that have received their DHCP-lease.
I don't know how many hours of lease-time you provide so these clients don't really benefit from the switchover you make on RouterOS. If their (only) DNS-server fails it is over & out.

Multiple DNS would be a / the only true "redundant solution" for your clients.

EDIT : Ah ok, so your Mikrotik IS already the DNS for your client and you are only forwarding to either Pi-hole (if operational) or some public upstream DNS in case of failure ? Or not ?
Because if your Mikrotik is in the DNS-chain, do you see in the Pi-hole stats the individual clients ? Aren't they all appearing come from 1 client "mikrotik" ?
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1747
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

Re: [Script] Automatically change DNS if Pi-hole is no longer working

Sun May 31, 2020 6:43 pm

Thanks for the script.

I do see a use for it in my case. I have a DoH server running separately on an MT running 6.47 beta. On my main MT Router i have one DNS point to that DoH MT router. If add a second DNS on main router, that will be used without going trough the DoH server. So I can use the script to test the if the DNS on the DoH router works, if not replace DNS with a working one.


Some tips.

If you do suggest to use local variable. I do not see in your script any reason to use global variable. It will just fill up the variable space.

Noe need for semicolon at the end of the line. Only needed when multiple commands are on the same line.

So:
: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={}
}
 
How to use Splunk to monitor your MikroTik Router(s)

MikroTik->Splunk
 
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1747
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

Re: [Script] Automatically change DNS if Pi-hole is no longer working

Sun May 31, 2020 7:15 pm

Here is my version of DoH server not working any more. Thanks again for the idea.
Added logging when things change. I love to log everything (see my signature)
:local currentDNS [/ip dns get server]
:local DoHDNS "192.168.20.10"
:local backupDNS "8.8.8.8,1.1.1.1"
:local testDomain "www.google.com"

:if ($currentDNS = $DoHDNS) do={
    :do {
        :resolve $testDomain server $DoHDNS
    } on-error={
        /ip dns set servers=$backupDNS
		:log info message="DNS_server=$backupDNS"
    }
} else={
    :do {
        :resolve $testDomain server $DoHDNS
        /ip dns set servers=$DoHDNS
		:log info message="DNS_server=$DoHDNS"
    } on-error={}
}
 
How to use Splunk to monitor your MikroTik Router(s)

MikroTik->Splunk
 
 
erkexzcx
just joined
Topic Author
Posts: 15
Joined: Mon Oct 07, 2019 11:42 pm

Re: [Script] Automatically change DNS if Pi-hole is no longer working

Sun May 31, 2020 7:44 pm

Thank you. I updated my initial comment with your suggestions. :)

Who is online

Users browsing this forum: No registered users and 15 guests