Skip to main content

A Python library for checking server availability and managing server configurations via SSH, with support for parallel execution using threading.

Project description

Starlyng SSH Connection Library

A Python library for checking server availability and managing server configurations via SSH.

Features

  • Check multiple servers' SSH availability in parallel using ping_servers
  • Execute commands across multiple servers using run_command_on_servers
  • Server configuration management through Server dataclass
  • Parallel execution using threading
  • Comprehensive error handling and logging
  • Utility functions for server management:
    • get_hostname: Generate hostnames from IP/port
    • get_host_id: Generate host IDs
    • get_ip_for_ssh: Format IPs for SSH connections

Prerequisites

Before you begin, ensure you have:

  • Python 3.9 or higher installed
  • SSH access to target servers
  • SSH key authentication configured (optional)

Installation

pip install starlyng-ssh-connection

Usage

Creating Servers

from ssh_connection.models import Server

# Create server instances for local network
servers = [
    Server(
        hostname="server01",
        ip="192.168.1.100",
        public_ip=False,
        ssh_key_path="~/.ssh/id_rsa",
        ssh_port=22,
        ssh_user="admin",
        ssh_vlan_id=10
    ),
    Server(
        hostname="server02",
        ip="192.168.1.101",
        public_ip=False,
        ssh_key_path="~/.ssh/id_rsa",
        ssh_port=22,
        ssh_user="admin",
        ssh_vlan_id=10
    )
]

# Create a public server instance
public_server = Server(
    hostname="public-server01",
    ip="203.0.113.10",
    public_ip=True,
    ssh_key_path="~/.ssh/id_rsa",
    ssh_port=12345,
    ssh_user="admin",
    ssh_vlan_id=0  # Not used for public IPs
)

Checking Server Availability

from ssh_connection.connections import ping_servers

# Check which servers are offline
offline_servers = ping_servers(servers)
if offline_servers:
    print("Offline servers:", offline_servers)
else:
    print("All servers are online!")

Running Commands on Servers

from ssh_connection.connections import run_command_on_servers

# Run a single command on all servers
results = run_command_on_servers(servers, "uptime")
for hostname, result in results.items():
    if result['error']:
        print(f"{hostname} error: {result['error']}")
    else:
        print(f"{hostname} output: {result['output']}")

Running Multiple Commands on Servers

from ssh_connection.connections import run_command_on_servers

# Run multiple commands in sequence
commands = [
    "hostname",
    "uptime",
    "df -h"
]
results = run_command_on_servers(servers, commands)
for hostname, result in results.items():
    if result['error']:
        print(f"{hostname} error: {result['error']}")
    else:
        print(f"{hostname} output: {result['output']}")

Working with Local Network Servers

from ssh_connection.utils import get_hostname, get_ip_for_ssh

# Generate hostname for a local server
hostname = get_hostname("server", "192.168.1.20", 22, public_ip=False)
print(f"Generated hostname: {hostname}")  # Output: server10

# Get SSH IP address with VLAN
ssh_ip = get_ip_for_ssh("192.168.1.100", vlan_id=10, public_ip=False)
print(f"SSH IP: {ssh_ip}")  # Output: 192.168.10.100

Working with Public Servers

from ssh_connection.utils import get_hostname, get_ip_for_ssh

# Generate hostname for a public server (uses port number)
public_hostname = get_hostname("server", "203.0.113.10", 12345, public_ip=True)
print(f"Public server hostname: {public_hostname}")  # Output: server45

# Get SSH IP for public server
public_ssh_ip = get_ip_for_ssh("203.0.113.10", vlan_id=0, public_ip=True)
print(f"Public SSH IP: {public_ssh_ip}")  # Output: 203.0.113.10

Contributing

  1. Fork the project
  2. Create your feature branch (git checkout -b feature/YourFeature)
  3. Commit your changes (git commit -am 'Add some YourFeature')
  4. Push to the branch (git push origin feature/YourFeature)
  5. Open a pull request

Contact

If you have any questions, please contact:

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

starlyng_ssh_connection-0.0.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

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

starlyng_ssh_connection-0.0.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: starlyng_ssh_connection-0.0.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.6

File hashes

Hashes for starlyng_ssh_connection-0.0.1.tar.gz
Algorithm Hash digest
SHA256 4238059d19d4d7530202a05a6c9f33c973660068ce0c87db3c841380a3c867e1
MD5 9a547dfd64260d3612b63ff8abc05c61
BLAKE2b-256 d643b4539592d10afb470760e87bec9995dfdbbfa7296e988523d4ab0ef7c8d5

See more details on using hashes here.

File details

Details for the file starlyng_ssh_connection-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for starlyng_ssh_connection-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4e80e53a42f79144a675e46bcff845d8c7a79345777e142d1a377519fa0d9801
MD5 81b4d3e0d322786f1bdeee8a79835717
BLAKE2b-256 8d8f4e3f5746338e64bef9d222caa2f7bfcf691b583b6744523466f9678f829e

See more details on using hashes here.

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