Eugene Amos e7e687df77 IP Checker
> Adding IP checker files and updating README
2023-12-05 18:03:54 -08:00

5.1 KiB

IP Checker

A script that monitors the current VPN IP of qbittorrent-openvpn container.
The script will compare your Internet Service Providers (ISP) IP Address
to the current IP address of container qbittorrent-openvpn.
The check will run at a specified time along with logging
the current IP Address of the qbittorrent-openvpn container.
If both ISP IP and VPN container IP match, that will mean
that qbittorrent is running on a non encrypted internet connection.
The script will then proceed to kill the qbittorrent container and send a notification
to either ntfy or pushover with an **URGENT** message.

Prerequisites

The following modules are required for the script to function. They will need to be installed first. Python ships with some core modules but others will need to be installed separately.

Standard Python Library Modules:

This script uses Python and multiple add on modules.

  • os: Provides a way to use operating system dependent functionality. Part of the standard library.

  • time: Used for time-related tasks. Included in the standard library.

  • json: Used for JSON serialization and deserialization. Part of the standard library.

  • logging: Used for logging events and messages. Standard library module.

  • datetime: Provides classes for manipulating dates and times. Standard library module.

  • subprocess: Used for running new applications or programs through Python. Part of the standard library.

  • logging.handlers: A component of the logging module, offers various handlers for logging. Standard library.

  • configparser: Renamed to ConfigParser in Python 3, this module is used for working with configuration files. Part of the standard library.

  • email.mime.text: Used for creating MIME objects of major type text. Part of the standard Python library under the email package.

Modules that Need to be Installed:

  • colorlog: This is an external library for colored logging output. You need to install it separately using pip.

  • requests: A popular HTTP library for Python, used for making HTTP requests. This is not included in the standard library and must be installed separately.

To install the external modules you will first need PIP installed on your server. PIP is the package manager for Python.

  1. See if pip is already installed. Open a terminal and type:

    pip -V
    
  2. If not, install it.

    python get-pip.py
    
  3. Once pip is installed you can install the external modules.

    pip install colorlog requests
    

Folder Structure

If you have not done so already, create a new folder called scripts in your ~home folder. This is where all your scripts will go.

Inside your scripts folder create a new folder called ip_checker

When done your folders structure should look like this: /home/[username]/scripts/ip_checker

Script

Get the source code from this repo and save it as ip_checker.py in the ip_checker folder created in the previous step.

You will also need to get the config file called ip_checker_config.ini. The .ini file will hold the credentials for pushover and ntfy. You can also set your notification preference for the script.

Next we need to change the permissions of these files so that the server can read them.

  1. For both ip_checker.py and ip_checker_config.ini right click then Properties.

  2. Click on Permissions tab at top.

  3. Make sure Owner & Group reflect your username with Read and Write access to all.

  4. Check the Execute box to allow executing the file as a program.

  5. Close out the window.

Run

Now we need to test and run the script.

Make sure your terminal is open to the location of the ip_checker.py file. Should be ~/scripts/ip_checker

Normally to run a Python script will would use python3 ip_checker.py

This will still work but because we have shebang set in the script we can just use ./ip_checker.py

You should now see [INFO] log with current VPN IP of the qbittorrent-openvpn container. In the script we set the run time with interval_seconds = 60. This setting tells the script to check the VPN IP address of the container every minute.

Automation

Now the script will need to be automated so that it starts the check continuously in the background. See system services to do so.