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.1.tar.gz (14.4 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.1-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

Details for the file osn_system_utils-0.0.1.tar.gz.

File metadata

  • Download URL: osn_system_utils-0.0.1.tar.gz
  • Upload date:
  • Size: 14.4 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.1.tar.gz
Algorithm Hash digest
SHA256 b68a9c2750a44e00b4816ce483624e610187f14939439730d324c9ab5fd77e52
MD5 cc162079062a9e8032dda81e36d62ec4
BLAKE2b-256 9f7aab871deb96005597b37c3059c41168370e52828637fc36d909bc3e6c2891

See more details on using hashes here.

Provenance

The following attestation bundles were made for osn_system_utils-0.0.1.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.1-py3-none-any.whl.

File metadata

File hashes

Hashes for osn_system_utils-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cdfc480d7ace2730e4f8625f17d6e63ffcc6442a4faf17635b50092f3411be71
MD5 54c2b2a90707ba58dc7315586458de36
BLAKE2b-256 5d8f94f4ee03dae91ac5ac472deb210ae39f9f93e8040d3edeada22ad9308e75

See more details on using hashes here.

Provenance

The following attestation bundles were made for osn_system_utils-0.0.1-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