A Python script that automatically tests and switches to the fastest available DNS server at regular intervals. Designed for Linux systems using NetworkManager.
- Python 3.x
- Linux with NetworkManager (
nmcli) digcommand (from dnsutils package)- Root privileges
# Install dependencies
sudo apt-get install dnsutils network-manager # Debian/Ubuntu
sudo yum install bind-utils NetworkManager # RHEL/CentOS
# Make script executable
chmod +x dns_switcher.pyDNS_SERVERS = {
"Google": ["8.8.8.8", "8.8.4.4"],
"Cloudflare": ["1.1.1.1", "1.0.0.1"],
# ... (17 other providers)
}CHECK_INTERVAL = 300 # 5 minutes (adjust as needed)sudo ./dns_switcher.pyPress Ctrl+C to stop the script
- Tests response times using
digcommand - Returns:
{"Provider": latency_ms, ...}
- Configures DNS via NetworkManager
- Returns:
Trueif success,Falseif failed
- Main control loop
- Handles testing, switching, and interval timing
==================================================
π Testing for fastest DNS (Interval: 300s)...
π Test Results:
Google: 24.53 ms
Cloudflare: 18.21 ms
Quad9: 32.67 ms
β‘ Fastest DNS changed to: Cloudflare (18.21 ms)
β
DNS changed to Cloudflare: 1.1.1.1, 1.0.0.1
| Issue | Solution |
|---|---|
| DNS changes fail | Run with sudo |
| No test results | Install dnsutils |
| Connection errors | Check NetworkManager |
MIT License - Free for modification and distribution
- Changes persist until network restart
- Tested on Ubuntu/Fedora
- Add providers by editing
DNS_SERVERSdictionary