The problem
I have got different DNS servers on my Windows computer, depending on the location I am currently at: If I am in the office then there are the company DNS servers. If I am at home, then my home DNS server should be used.
Sadly, WSL seems to be unable to update the DNS servers on it’s own. I am not quite sure why that is, but I found a way around it.
Requirements
I will use an external library: dns.resolver. I installed that one via Conda: conda install -n dns-resolver dnspython.
The code
| |
The concept
I will first let Powershell list all the DNS servers that are currently defined in some networks. On my company device, not all those DNS servers are actually reachable - for example, the VPN network has got predefined DNS servers which will also be defined here but unavailable if I am not connected to VPN.
So the second step is the following: Loop through the list of all those DNS servers and try to resolve some domain name. If so, write the DNS record into the /etc/resolv.conf file.
Note the set(servers) in line $23$: For different interfaces (for example, LAN and WiFi), the same DNS servers may be used. With the set, I won’t check the same DNS server twice.