This library provides a set of Python abstractions for common system administration tasks on Linux and Windows, as well as utilities for network port management.
Project description
osn-system-utils: Cross-platform system command wrappers and network utilities
This library provides a set of Python abstractions for common system administration tasks on Linux and Windows, as well as utilities for network port management. It allows developers to interact with system commands like shutdown, kill, netstat, and ss using structured, type-hinted Python functions instead of raw shell execution strings.
Technologies
Key Features
- Cross-Platform Network Utilities:
- Find random or minimum free ports on localhost.
- Map PIDs to used ports and addresses.
- Identify busy and free ports within specific ranges.
- Linux System Wrappers:
killandpkillfor process management.shutdownfor power management.ssfor detailed socket statistics.
- Windows System Wrappers:
taskkillfor process termination.shutdownfor power management.netstatfor network statistics.
- Type Safety:
- Input validation for command arguments (modes, modifiers, protocols).
- Custom exception handling for command execution failures.
Installation
-
Install library:
-
With pip:
pip install osn-system-utils
With pip (beta versions):
pip install -i https://test.pypi.org/simple/ osn-system-utils
-
With git:
pip install git+https://github.com/oddshellnick/osn-system-utils.git
(Ensure you have git installed)
-
-
Install the required Python packages using pip:
pip install -r requirements.txt
Usage
Here are some examples of how to use osn-system-utils:
Network Utilities
Retrieve a free port on localhost or check which ports are busy.
from osn_system_utils.api.network import get_random_localhost_free_port, get_localhost_pids_with_ports
# Get a random free port
port = get_random_localhost_free_port()
print(f"Found free port: {port}")
# Get mapping of PIDs to ports
pid_map = get_localhost_pids_with_ports()
print(pid_map)
Linux Process Management
Kill a process by name pattern on a Linux system.
from osn_system_utils.linux.kill import run_pkill
try:
# Kill the newest process matching "python_script"
output = run_pkill(pattern="python_script", modifiers=["newest"])
print("Process killed.")
except Exception as e:
print(f"Error: {e}")
Windows Task Management
Forcefully terminate a task by image name on Windows.
from osn_system_utils.windows.taskkill import run_taskkill
try:
# Force kill notepad.exe
run_taskkill(image_names=["notepad.exe"], modifiers=["force"])
print("Notepad closed.")
except ValueError as e:
print(e)
Classes and Functions
Core Utilities (osn_system_utils)
exceptionsCommandExecutionError- Exception raised when a command execution fails.
utilsvalidate_parameter(...)- Validates that a value is present in a list of available values.run_command(...)- Executes a shell command provided as a list of parts.deduplicate_list(...)- Removes duplicate items from a list while preserving order.
Network API (osn_system_utils.api.network)
get_random_localhost_free_port(...)- Finds a random free port on localhost by binding to port 0.get_localhost_pids_with_ports(...)- Retrieves a mapping of PIDs to lists of ports they are using on localhost.get_localhost_pids_with_addresses(...)- Retrieves a mapping of PIDs to lists of formatted addresses (IP:Port) on localhost.get_localhost_minimum_free_port(...)- Finds the minimum free port from a specific set or the default range.get_localhost_busy_ports(...)- Retrieves a sorted list of ports currently in use on localhost.get_localhost_free_ports(...)- Retrieves a sorted list of all free ports in the default range on localhost.
Linux Wrappers (osn_system_utils.linux)
killbuild_pkill(...)- Builds the pkill command list.run_pkill(...)- Executes the pkill command.build_kill(...)- Builds the kill command list.run_kill(...)- Executes the kill command for specified PIDs.
shutdownbuild_shutdown(...)- Builds the shutdown command arguments.run_shutdown(...)- Executes the shutdown command.
ssbuild_ss(...)- Builds the ss (socket statistics) command arguments.run_ss(...)- Executes the ss command.
Windows Wrappers (osn_system_utils.windows)
netstatbuild_netstat(...)- Builds the netstat command arguments.run_netstat(...)- Executes the netstat command.
shutdownbuild_shutdown(...)- Builds the shutdown command arguments (Windows specific).run_shutdown(...)- Executes the shutdown command.
taskkillbuild_taskkill(...)- Builds the taskkill command arguments.run_taskkill(...)- Executes the taskkill command.
Future Notes
- Implementation of MacOS specific command wrappers.
- Asynchronous versions of command execution functions.
- Wrappers for many other commands.
- Integration with remote command execution via SSH.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file osn_system_utils-0.0.0.tar.gz.
File metadata
- Download URL: osn_system_utils-0.0.0.tar.gz
- Upload date:
- Size: 11.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2610df6948b7562c92229fc811774d3d228d9bc6d4fc6d96d991592ae3e1c22e
|
|
| MD5 |
d3ad1caff7cb66d4d26fda0f4e6b0739
|
|
| BLAKE2b-256 |
8996f934321decee039d70cb0fa9722ddaa7f56a71a44c251871e4d1a58c2178
|
Provenance
The following attestation bundles were made for osn_system_utils-0.0.0.tar.gz:
Publisher:
python-publish.yml on oddshellnick/osn-system-utils
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
osn_system_utils-0.0.0.tar.gz -
Subject digest:
2610df6948b7562c92229fc811774d3d228d9bc6d4fc6d96d991592ae3e1c22e - Sigstore transparency entry: 813308299
- Sigstore integration time:
-
Permalink:
oddshellnick/osn-system-utils@04f7f97a9cc1a0c128794134d848bca48faf9d07 -
Branch / Tag:
refs/tags/v0.0.0 - Owner: https://github.com/oddshellnick
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@04f7f97a9cc1a0c128794134d848bca48faf9d07 -
Trigger Event:
release
-
Statement type:
File details
Details for the file osn_system_utils-0.0.0-py3-none-any.whl.
File metadata
- Download URL: osn_system_utils-0.0.0-py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
63e5b8a17f9f992e165d3332b3a4da22eed4c1d8329338f00fb832c715badf73
|
|
| MD5 |
94bf5991cf57ac4d0e2a811b2f7b2238
|
|
| BLAKE2b-256 |
03078406e0df9e3df0e798b9a36b2d757bdc7f0b6351f4b02af34828ce5fd791
|
Provenance
The following attestation bundles were made for osn_system_utils-0.0.0-py3-none-any.whl:
Publisher:
python-publish.yml on oddshellnick/osn-system-utils
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
osn_system_utils-0.0.0-py3-none-any.whl -
Subject digest:
63e5b8a17f9f992e165d3332b3a4da22eed4c1d8329338f00fb832c715badf73 - Sigstore transparency entry: 813308300
- Sigstore integration time:
-
Permalink:
oddshellnick/osn-system-utils@04f7f97a9cc1a0c128794134d848bca48faf9d07 -
Branch / Tag:
refs/tags/v0.0.0 - Owner: https://github.com/oddshellnick
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@04f7f97a9cc1a0c128794134d848bca48faf9d07 -
Trigger Event:
release
-
Statement type: