This script monitors the average ping to a specified host (e.g., Cloudflare DNS) and dynamically changes the DNS settings based on the ping performance. If the average ping exceeds a high threshold for a specified number of consecutive checks, it switches to an alternative DNS (e.g., Google DNS). Conversely, if the ping falls below a low threshold for consecutive checks, it switches back to the original DNS.
- The script is written in Bash and requires a Unix-like operating system (e.g., Linux, MacOS).
- Administrative privileges may be required to change DNS settings.
-
Clone the repository:
git clone https://github.com/rhakbari/dns-monitor-script.git
-
Make the script executable:
chmod +x dns_monitor.sh
-
Run the script in the background:
./dns_monitor.sh &
This guide demonstrates how to set up a systemd service to run a Python script automatically on every boot in Debian.
- Debian-based distribution (e.g., Debian, Ubuntu)
- Python script to run on boot
-
Create a systemd Service File:
-
Create a
.service
file for your Python script. For example, let's assume your Python script is namedtplink_dns.py
, and it's located in/path/to/script
. -
Open a terminal and create a file named
myscript.service
in the/etc/systemd/system/
directory:sudo nano /etc/systemd/system/myscript.service
-
Add the following content to the
myscript.service
file:[Unit] Description=My Python Script Service After=multi-user.target [Service] Type=simple ExecStart=/usr/bin/python3 /path/to/script/tplink_dns.py Restart=always [Install] WantedBy=multi-user.target
-
Replace
/path/to/script
with the actual path to your Python script.
-
-
Enable the Service:
-
After creating the service file, reload systemd and enable the service:
sudo systemctl daemon-reload sudo systemctl enable myscript.service
-
-
Start the Service:
-
Start the service immediately without rebooting:
sudo systemctl start myscript.service
-
-
Verification:
-
You can check the status of the service using:
sudo systemctl status myscript.service
-
Ensure that your script has the necessary permissions to execute and read any files it needs.
-