What's new

DNS Script ideas... Im bored

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

Maverickcdn

Senior Member
Winter is upon us and cold weather brings inane indoor projects...

Im learning shell coding and would like some input on something Im wasting my time with for fun (at least its fun to me)

I run a PI as a server on my network and it handles most everything including DNS with DoH through DNSCRYPT, also I provide filtered Internet access to roomates, instead of DNS on my router Id prefer a test/failover option. Please dont tell me to just run DNS from my router, Im doing this as a learning project.

As I've read and experienced with DNSMASQ on my router the option 'strict-order' works.... but it doesnt work, flaky at best anyway. I find personally when primary dns fails and dnsmasq moves to the secondary it can take a really long time for it to revert back to primary when it is up again (ie, my PI). I tinker a lot and the PI can be up or down for various periods of time on a given day that Im tinkering with it.

Looking for some community input on what would be the best way to script an 'if statement' to confirm DNS resolving is still up.

Id like to make my script available in the end but dont want to install any additional packages so Im thinking Im only left with curl (currently using) and nslookup. I have a timer setting to filter cached responses, but thinking my current curl method is far from ideal. And with nslookup I cant figure out how to set a timeout option when dns is failing, currently if a lookup is failing nslookup can hang for a long time before giving a result.

Of course most Internet searches recommend people use dig, which is not an option as it requires installing extra packages, something Id like to avoid.

Just wondering if anyone has some thoughts on what we be a fairly reliable way to test DNS resolution in a script from the router??
 
Thanks for the tip Dave, but I ended up figuring out a watchdog for nslookup and stuck with that, mainly as you can target specific DNS servers with nslookup which was convenient.

Attached is the script I wrote for a DNS Failover if anyone else can find it useful. Like I mentioned in my first post, this is for people that run a DNS server on their LAN (raspberry pi, etc)

https://www.dropbox.com/s/p2l3tz7dkqbrutz/dnscheck.sh?dl=0

Copy dnscheck.sh to /jffs/scripts, do the chmod a+rx and on first run it will prompt you to create the config file (DNS IPs and Logname etc), after that you can manually run it with 'sh dnscheck.sh' to see it working.

This script overwrites /jffs/configs/dnsmasq.conf.add so if you are using that option backup your original file first. My script has 3 optional entries for dnsmasq options like quiet-dhcp etc. that get written to that file.

Requires
jffs enabled and useable
primary/secondary DNS IPs
A logname
dnsmasq.conf.add options (upto 3)
cron job in init-start to run script every 2/5/10 mins whatever you want (I use 2 mins)
must use 'cron' argument in init-start to tell script it is being run by cron )ie. cru a dnscheck "*/2 * * * * /jffs/scripts/dnscheck.sh cron"
Router GUI WAN should have all DNS options NOT selected. And no DNS servers entered
Router GUI LAN page under DHCP should have NO DNS servers and only advertise router IP selected.
Setting these two above will ensure clients connected to the router are doing all dns requests through the router config (unless those clients are manually configured otherwise)

Script checks to see DNS is resolving against a random site from a list, ignores cached responses (upto 10 times then waits for cron run again), if dns resolution fails it switches to your secondary DNS and runs again with cron. If primary DNS can't resolve it stays with secondary DNS (ie 1.1.1.1 or other reliable DNS)

Can be a little spammy to your router logs when primary DNS is down depending on cron run frequency but at least if your primary DNS is down and you check your logs you certainly wont miss the issue. haha

Dunno, it works well for me, was a fun project anyway and I can still continue to tinker and refine it more if anyone finds it useful for them aswell.
 

Similar threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top