Internet Watchdog on Raspberry Pi

Published: March 16, 2021

I created a script that monitors whether your device has a working internet connection. If it does not, it restarts the router, and if the problem continues, the device itself will reboot. This helps keep access to your network and device stable when the internet connection fails. You will learn how to create an OpenWrt watchdog or router watchdog using a simple Python script.

Requirements

  • A Linux-based device (such as Raspberry Pi ) with Python 3 installed

  • One of the supported routers: Cisco EPC3925 , Nokia G-240W-C, or another router with OpenWrt installed

  • The following Python packages installed on the device: requests and paramiko (the latter only for routers running OpenWrt ). The http.client module is part of the Python standard library and does not need to be installed.

  • Access credentials for your device: login and password

  • If you use a router with OpenWrt , make sure SSH access is enabled and a password is set

  • The script saves the date of the last restart to a file in its own location to avoid a restart loop, so make sure it has write permissions there

Installation

Install the required Python packages.

pip3 install requests paramiko

Clone the repository to any directory on your device, for example your home directory.

cd
git clone https://github.com/paulomac1000/Raspberry-Internet-Watchdog

Go to the script directory.

cd Raspberry-Internet-Watchdog

Configure the main script.

nano watchdog.internet.py

Go to line 44 (the code below) and uncomment the call to the restart script for your router.

# Uncomment line for Your router and update access data in selected file
#os.system('python3 Cisco_EPC3925.py')
#os.system('python3 Nokia_G-240W-C.py')
#os.system('python3 OpenWrt.py')

Close the file using ctrl+x and save your changes.

Next, configure the script that will restart the router:

If you use a Cisco EPC3925 router:

Open the Cisco_EPC3925.py script

nano Cisco_EPC3925.py

And set the appropriate access details, such as the URL, login, and password. The values provided in the script are the defaults for routers supplied by the Inea ISP.

url = "192.168.0.1"
username = "homeu"
password = "homep"

Close the file using ctrl+x and save your changes.

If you use a Nokia G-240W-C router:

This is a little more complicated. After logging in, you need to obtain the authorization tokens generated by the router. Don't be discouraged—it is easy to find them. Unfortunately, I could not rewrite the logic that generated them because it used a package that was not available for Pythona.

Of course, if you use an Inea router with the default password, you do not need to change anything in the script. Otherwise:

  • go to the router login page,

  • open the developer tools (press F12 in chrome) and select the "Network" tab,

  • select the Preserve logs checkbox in the developer tools,

  • log in to the router,

  • find the request named login.cgi going to http://192.168.1.1/login.cgi and open it,

  • at the very bottom you will see the "Form Data" values; copy the values of the ct and ck parameters.

Open the Nokia_G-240W-C.py script

nano Nokia_G-240W-C.py

And set the appropriate access details, such as url, ct, and ck. The values provided in the script are the defaults for routers supplied by the Inea ISP.

url = "192.168.1.1"
ct = "2Awp6Rntn24AtFDkwCioWarQFk734lYGf0lIz6EoJdv1E741j-_mChCqgJzmSby4OvHDpyTcIs87PeKeKxkEpRS7vYEoLFX3_LhVuF4fWE9SP8pfhR8I-jsynfwfpGDWqX5mXBClQgnjudj0_evkf0UJZYCaiZgmEI3G8OwV5BqP5nDreYL1RmS_9xNgfjdMu6LTALtiCqrg_0yi-XF4Kr8HAsK0UvDXugmk8nNoi8eDWk_ELGVwDcuJtxsRgbzN8yJXvqJD_Iqnf_A1G7yq7T9vLRCYb5loHljS5GGC7dI"
ck = "mqdh47u9tZ4pMNyMkK5bSOvEdN8XgeRwplu24qLG7fwDPRcxNPQMy0HVbp0sXb7cSqe6EUoEMIHt1btDghNp16AogCS531fNeBqT4Vt1bVnONu6cIash2yvjOCZtInruarFsuerxsxv8K7-80cYTvQKgdsqSUH2CZ0BSGH_D7yQ."

Close the file using ctrl+x and save your changes.

If you use another router with OpenWrt installed:

Open the OpenWrt.py script

nano OpenWrt.py

And set the appropriate access details, such as the URL, login, and password. Make sure SSH access is enabled and a password is set.

url = "192.168.1.1"
username = "root"
password = "password"

Close the file using ctrl+x and save your changes.

Usage

To run the script, execute it from the command line.

python3 watchdog.internet.py

To run the script automatically, add it to your crontab.

crontab -e

And add the script call. The following command will run the script every 15 minutes.

*/15 * * * * python3 /home/pi/Raspberry-Internet-Watchdog/watchdog.internet.py

Comments (0)

No comments yet.

Add a comment

Comments are published after moderation. Your e-mail address stays private.