Skip to main content

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

Name Badge Description
Python Python The core language used for implementing the wrappers and logic.
Psutil Psutil Used for retrieving network connection details and mapping PIDs to ports.
Subprocess Subprocess Used to execute the underlying system shell commands.
Socket Socket Used for binding checks to identify free ports on localhost.

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:
    • kill and pkill for process management.
    • shutdown for power management.
    • ss for detailed socket statistics.
  • Windows System Wrappers:
    • taskkill for process termination.
    • shutdown for power management.
    • netstat for network statistics.
  • Type Safety:
    • Input validation for command arguments (modes, modifiers, protocols).
    • Custom exception handling for command execution failures.

Installation

  1. 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)

  2. 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)

  • exceptions
    • CommandExecutionError - Exception raised when a command execution fails.
  • utils
    • validate_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)

  • kill
    • build_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.
  • shutdown
    • build_shutdown(...) - Builds the shutdown command arguments.
    • run_shutdown(...) - Executes the shutdown command.
  • ss
    • build_ss(...) - Builds the ss (socket statistics) command arguments.
    • run_ss(...) - Executes the ss command.

Windows Wrappers (osn_system_utils.windows)

  • netstat
    • build_netstat(...) - Builds the netstat command arguments.
    • run_netstat(...) - Executes the netstat command.
  • shutdown
    • build_shutdown(...) - Builds the shutdown command arguments (Windows specific).
    • run_shutdown(...) - Executes the shutdown command.
  • taskkill
    • build_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

osn_system_utils-0.0.0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

osn_system_utils-0.0.0-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

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

Hashes for osn_system_utils-0.0.0.tar.gz
Algorithm Hash digest
SHA256 2610df6948b7562c92229fc811774d3d228d9bc6d4fc6d96d991592ae3e1c22e
MD5 d3ad1caff7cb66d4d26fda0f4e6b0739
BLAKE2b-256 8996f934321decee039d70cb0fa9722ddaa7f56a71a44c251871e4d1a58c2178

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file osn_system_utils-0.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for osn_system_utils-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 63e5b8a17f9f992e165d3332b3a4da22eed4c1d8329338f00fb832c715badf73
MD5 94bf5991cf57ac4d0e2a811b2f7b2238
BLAKE2b-256 03078406e0df9e3df0e798b9a36b2d757bdc7f0b6351f4b02af34828ce5fd791

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page