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: A comprehensive cross-platform library for system process and network management.

This library provides a high-level API for managing system processes, discovering network ports, and executing OS-specific commands like shutdown or socket statistics on both Linux and Windows.

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

  • Network Management
    • Find free ports on localhost using various strategies (min, max, random).
    • Map PIDs to active ports or formatted addresses.
    • Check for busy and free ports within specific ranges.
  • Process Management
    • Kill processes by name or PID with support for process trees.
    • Query process tables with advanced filtering (regex, equality, numeric ranges).
    • Cross-platform check for process existence.
  • OS-Specific Wrappers
    • Linux: High-level wrappers for pkill, kill, shutdown, and ss (socket statistics).
    • Windows: High-level wrappers for taskkill, shutdown, and netstat.

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 Port Discovery

from osn_system_utils.api.network import get_localhost_free_port_of

# Find the first available free port in the default range
port = get_localhost_free_port_of(on_candidates="min")
print(f"Available port: {port}")

# Find a random free port from a specific list
specific_port = get_localhost_free_port_of(ports_to_check=[8080, 8081, 9000], on_candidates="random")

Advanced Process Filtering

from osn_system_utils.api.process import get_process_table
import re

# Get all python processes using more than 100MB of memory
filters = {
    "memory_info.rss": 100 * 1024 * 1024
}
for proc in get_process_table(above_filter=filters):
    print(proc)

Linux Socket Statistics (ss)

from osn_system_utils.linux.ss import run_ss

# Get a summary of all listening TCP sockets
output = run_ss(mode="list", scope="listening", protocols=["tcp"])
print(output)

Classes and Functions

General Utilities (osn_system_utils.utils)

  • validate_parameter(...): Validates that a value exists within a sequence of allowed values.
  • run_command(...): Executes a shell command and returns the stdout.
  • deduplicate_list(...): Removes duplicates from an iterable while maintaining order.
  • CommandExecutionError: Exception raised when a system command fails.

Network API (osn_system_utils.api.network)

  • get_random_localhost_free_port()
  • get_localhost_pids_with_ports()
  • get_localhost_pids_with_addresses()
  • get_localhost_busy_ports()
  • get_localhost_free_ports()
  • get_localhost_free_port_of(...)

Process API (osn_system_utils.api.process)

  • kill_processes_by_name(...)
  • kill_process_by_pid(...)
  • get_process_table(...)
  • check_process_exists_by_pid(...)
  • check_process_exists_by_name(...)

Linux System Wrappers (osn_system_utils.linux)

  • kill:
    • run_pkill(...): Execute Linux pkill.
    • run_kill(...): Execute Linux kill for specific PIDs.
  • shutdown:
    • run_shutdown(...): Manage system power (reboot, poweroff, etc.).
  • ss:
    • run_ss(...): Execute socket statistics command.

Windows System Wrappers (osn_system_utils.windows)

  • netstat:
    • run_netstat(...): Execute Windows netstat with various flags.
  • shutdown:
    • run_shutdown(...): Manage Windows system power and restart behavior.
  • taskkill:
    • run_taskkill(...): Terminate tasks by PID or Image Name.

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.2.tar.gz (14.6 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.2-py3-none-any.whl (17.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: osn_system_utils-0.0.2.tar.gz
  • Upload date:
  • Size: 14.6 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.2.tar.gz
Algorithm Hash digest
SHA256 456b2b30ebc9b56302c75522ef8e0cd8ae430197a2654e4bdb2dcc3e08d84a40
MD5 1ff0c6bc9785a432984edc3f1903b843
BLAKE2b-256 703bbd85d5f5238e878971b86bd8ccb279486297c3cb094995e795ec399cd674

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for osn_system_utils-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9d2c729bd24ffef5a1d7e1afbd619f7e612dfe649464408ec9443101a5ea22dc
MD5 561c4887662e522b82d02ceb9afbfd1b
BLAKE2b-256 cbf9cb58204d41f76785d4ceef071a1afa48f072036008cfe415717ec794b623

See more details on using hashes here.

Provenance

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