118 lines
5.1 KiB
Markdown
118 lines
5.1 KiB
Markdown
<h1 align="center">IP Checker</h1>
|
|
|
|
<p align="center">
|
|
A script that monitors the current <strong>VPN IP</strong> of <code>qbittorrent-openvpn</code> container.
|
|
<br />
|
|
The script will compare your <strong>Internet Service Providers (ISP) IP Address</strong>
|
|
<br />
|
|
to the <strong>current IP address</strong> of container <code>qbittorrent-openvpn</code>.
|
|
<br />
|
|
The check will run at a specified time along with logging
|
|
<br />
|
|
the current IP Address of the <code>qbittorrent-openvpn</code> container.
|
|
<br />
|
|
If both <strong>ISP IP</strong> and <strong>VPN container IP</strong> match, that will mean
|
|
<br />
|
|
that <strong>qbittorrent</strong> is running on a non encrypted internet connection.
|
|
<br />
|
|
The script will then proceed to kill the <code>qbittorrent</code> container and send a notification
|
|
<br />
|
|
to either <a href=" https://ntfy.sh" target="blank">ntfy</a> or <a href="https://pushover.net/" target="blank">pushover</a> with an <em><strong>**URGENT**</strong></em> message.
|
|
<br />
|
|
</p>
|
|
|
|
## 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:
|
|
|
|
* **<a href="https://pypi.org/project/colorlog/" target="_blank">colorlog:</a>** This is an external library for colored logging output. You need to install it separately using pip.
|
|
|
|
* **<a href="https://pypi.org/project/requests/" target="_blank">requests:</a>** 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. **<a href="https://pypi.org/project/pip/" target="_blank">PIP</a>** is the package manager for Python.
|
|
|
|
1. See if **pip** is already installed. Open a terminal and type:
|
|
|
|
```bash
|
|
pip -V
|
|
```
|
|
|
|
2. If not, install it.
|
|
|
|
```bash
|
|
python get-pip.py
|
|
```
|
|
|
|
3. Once **pip** is installed you can install the external modules.
|
|
|
|
```bash
|
|
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 <a href="https://linuxhandbook.com/shebang" target="blank">shebang</a> 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 <a href="https://wiki.euronvault.com/services" target="blank">system services</a> to do so.
|