I am using OpenVPN to connect to a VPN provider.
When OpenWRT boots, it gets the ISP's DNS servers through DHCP from my modem. Dnsmasq then uses those to setup /tmp/resolv.conf.auto
.Next, openvpn uses the VPN provider's DDNS name to discover the best performing server. It connects and pushes the server's DNS through the tunnel. I set the following script in my openvpn config (which I found on the net):
#!/bin/sh
mv /tmp/resolv.conf.auto /tmp/resolv.conf.auto.hold
echo $foreign_option_1 | sed -e 's/dhcp-option DOMAIN/domain/g' -e 's/dhcp-option DNS/nameserver/g' > /tmp/resolv.conf.auto
echo $foreign_option_2 | sed -e 's/dhcp-option DOMAIN/domain/g' -e 's/dhcp-option DNS/nameserver/g' >> /tmp/resolv.conf.auto
echo $foreign_option_3 | sed -e 's/dhcp-option DOMAIN/domain/g' -e 's/dhcp-option DNS/nameserver/g' >> /tmp/resolv.conf.auto
This works like a charm and my /tmp/resolv.conf.auto
changes.
However, dnsmasq will reset the config back to my ISP's servers periodically. At least when it does a DHCP refresh on the WAN-connection, but also on other moments I haven't quite pinpointed yet.
I am looking for some way to write a shell script that catches that and changes it back to the VPN's server, since my network loses all name resolution otherwise.
So far, my idea is to write a 'dumb'-script that runs every minute as a cron-job, checks if the tunnel is up, then checks if the resolv.conf.auto
contains a 10.x.x.x nameserver. If not, it will overwrite the file with the VPN's resolver (I would add a cp
to the above script at the end to backup the VPN's resolv.conf
).
It would of course be nicer if the script can somehow be triggered by dnsmasq's periodical updates.
Problem is, I can do magic in PowerShell, but there's no PowerShell opkg (can that even be done?), so I need some help writing a shell-script that works for this. I would really appreciate it.