A virtual environment isolates dependencies for this project.
Windows:
python -m venv venv
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
venv\Scripts\activate
Set-ExecutionPolicy Restricted
Linux:
python3 -m venv venv
source venv/bin/activate
Install the required libraries using requirements.txt
.
pip install -r requirements.txt
To compile the Python script into an executable:
-
Install PyInstaller:
pip install pyinstaller
-
Compile the Script:
pyinstaller --onefile ping.py
-
Locate the Executable: The compiled executable will be available in the
dist
directory asping.exe
.
Before installing the script as a service, you need to modify the ping.env.example
file to configure the environment variables.
-
Open the
dist/ping.env.example
file in a text editor. -
Set the appropriate values for the environment variables:
DNS=8.8.4.4 # Set your DNS server (defaults to 8.8.8.8 if not set) GATEWAY=192.168.0.1 # Set your gateway IP (defaults to 192.168.1.1 if not set) EXTRA=1.1.1.1 # Set an additional server IP or leave blank to skip
-
Save the modified file as
ping.env
in the same directory as the executable (dist/ping.exe
).
To run the script directly on Linux:
-
Ensure the environment is active:
source venv/bin/activate
-
Run the script:
python ping_script.py
To run the compiled executable (ping.exe
) as a service on Windows using NSSM:
-
Download and Install NSSM:
- Go to the NSSM official website to download the appropriate version for your system (32-bit or 64-bit).
- Extract and place the
nssm.exe
file in a directory of your choice, or add it to your system’sPATH
.
-
Install the Service: Open a Command Prompt with administrative privileges and run the following command to install the service:
nssm install tridz-ping "C:\path\to\ping.exe"
Replace
"C:\path\to\ping.exe"
with the actual path to yourping.exe
file. The script expects ping.env in the same path -
Start the Service: To start the service, run:
nssm start tridz-ping
-
Stop the Service: To stop the service, run:
nssm stop tridz-ping
-
Uninstall the Service: To remove the service, run:
nssm remove tridz-ping
Ensure the dependencies used in your project are listed. For this script, it might look like this:
ping3
python-dotenv