[Q] Code to verify and set Custom DNS

Hi!

As I’m new with openwrt then I would like to ask if someone could check and validate my code.

Goal: I look to verify a/o set my custom DNS if ever these are different
Implementation: I use the /etc/rc.local file to run my code at the end of the boot process
Devices: AR150 and MT300N-V2

Code:

#My required custom DNS address
cust_dns1='50.90.40.80'
cust_dns2='50.90.40.81'

#Retrieve the currently used DNS address
used_dns=$(uci get dhcp.@dnsmasq[0].server)
#Or  used_dns=$(echo $(cat /tmp/resolv.conf.auto | grep 'nameserver' | cut -d' ' -f2))

#Verify if the used DNS matches my custom DNS, if not then set and apply my custom DNS
[ "$used_dns" != "$cust_dns1 $cust_dns2" ] && {
uci set dhcp.@dnsmasq[0].server=$cust_dns1
uci add_list dhcp.@dnsmasq[0].server=$cust_dns2
uci set network.wan.dns="$cust_dns1 $cust_dns2"
uci set network.wwan.dns="$cust_dns1 $cust_dns2"
uci commit
/etc/init.d/dnsmasq restart
/etc/init.d/network reload
}

 

So far the code does the task, however I prefer to ask for validation so as to be sure that the DNS are set and applied correctly.

Thanks in advance

@luisblop

there are some problem in my test, maybe our network environment are different. my device is under a main router and use dhcp.

so there is a error when retrieve the currently used DNS address, like this:

root@GL-AR300M:/# uci get dhcp.@dnsmasq[0].server

uci: Entry not found

so I think is there a better way to get DNS?

Hi tangxiusi,

I use also the mini-router the same way with dhcp (router behind router), maybe the uci entry is a bit different in your device, I see you have an AR300M.

Can you please check your current uci dhcp values with the command “uci show dhcp”? Normally you should find one entry with your custom DNS address. Otherwise your device is not using dnsmasq to resolve public dns queries.

I think the most reliable will be if someone can post and share the script that is used when clicking the apply button inside the GUI’s Custom DNS settings.

 

hi luisblop

I know what you mean, I only want to say if your device used dhcp method the uci get can’t obtain the dns.

So I’m also looking for a more reliable way to get DNS.

@tangxiusi

Have you tried the following commands to retrieve your currently used DNS?

cat /etc/config/dhcp | grep 'list server' | cut -d\' -f2
or
cat /tmp/resolv.conf.auto | grep 'nameserver' | cut -d' ' -f2

 

This should work for custom DNS. Otherwise if you want to retrieve the DNS from your ISP that’s another question. In my case, I’m looking precisely for the right commands and their order to set and apply custom DNS address.

@luisblop

thanks very much and the last way your provide can get the obtain dns well. so I use the command

cat /tmp/resolv.conf.auto | grep ‘nameserver’ | cut -d’ ’ -f2

replace the code your write before , now it work well

Glad to hear that it works for you too

However I’m just a newbie with openwrt and uci so I’m not 100% sure if my code is reliable in all situations. That’s why it would be nice if an expert could confirm its validity.

I’ve put the working alternative command to retrieve the DNS in the initial code above.