Skip to main content

Automatically reconnect SSH sessions

Project description

PyAutoSSH

Python 3.10+ License: MIT

A Python tool that automatically reconnects SSH sessions after connection interruptions. Perfect for maintaining persistent connections when using terminal multiplexers like screen or tmux on remote hosts.

Features

  • Automatic reconnection on connection loss
  • Compatible with existing SSH configurations
  • Minimal dependencies

Prerequisites

  • Python 3.10 or newer
  • SSH client (ssh command available in PATH)
  • Configured SSH key-based authentication (recommended)

Installation

# Via pip (or pipx / uvx)
pip install pyautossh

# From source
git clone https://github.com/chrisdeutsch/pyautossh
cd pyautossh
pip install .

Usage

Basic Usage

pyautossh <args forwarded to ssh>

# For example
pyautossh -t user@hostname tmux new -A -s session_name

PyAutoSSH Options

PyAutoSSH accepts several specific options that control its behavior:

# Set maximum connection attempts before giving up
pyautossh --autossh-max-connection-attempts 10 user@hostname

# Set delay between reconnection attempts (in seconds)
pyautossh --autossh-reconnect-delay 5 user@hostname

# Enable verbose logging for debugging
pyautossh --autossh-verbose user@hostname

These options must be specified before any SSH arguments. All other arguments are passed directly to the SSH client.

Advanced Configuration

Create an SSH config entry in ~/.ssh/config:

Host hostname-tmux
    User username
    HostName hostname
    RequestTTY yes
    RemoteCommand tmux new-session -A -s base
    ServerAliveInterval 5
    ServerAliveCountMax 1

Then simply connect using:

pyautossh hostname-tmux

The parameters

  • ServerAliveInterval: Time interval (in seconds) for sending keep-alive messages
  • ServerAliveCountMax: Number of keep-alive messages that can be lost before disconnecting
  • RequestTTY: Forces TTY allocation (required for interactive sessions)

can be configured depending on how aggressively you want to reconnect.

Tips

  1. Use key-based authentication
  2. Configure SSH agent for passwordless authentication with passphrase-protected keys
  3. Adjust keep-alive settings based on how aggressive you want to reconnect
  4. Use terminal multiplexers (e.g. tmux or screen) for session persistence

Related Projects

  • autossh - The original AutoSSH by Carson Harding

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

pyautossh-0.3.1.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

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

pyautossh-0.3.1-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file pyautossh-0.3.1.tar.gz.

File metadata

  • Download URL: pyautossh-0.3.1.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pyautossh-0.3.1.tar.gz
Algorithm Hash digest
SHA256 a45b20c69170ff181cace03e6d6070f86a8300480f1add342519b4c27d858a13
MD5 a44b36b7aa3f001d965d27ecad578fdf
BLAKE2b-256 aa37fa47629f8d56f02d021a7dcc12b4a9d6ec4798400aa65e27eae254993e5f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyautossh-0.3.1.tar.gz:

Publisher: publish.yaml on chrisdeutsch/pyautossh

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

File details

Details for the file pyautossh-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: pyautossh-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pyautossh-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 94272d8ede269e876dca1e8c4d4dd312f1aebaa8569c14e2f7950df82b3734e1
MD5 90ca874023d3d3588e7b424793e9ddf2
BLAKE2b-256 e9c1c53e35bbb1f11ad532f2a3b64b35ab08a56b80fb7515b61535a7cc1ec255

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyautossh-0.3.1-py3-none-any.whl:

Publisher: publish.yaml on chrisdeutsch/pyautossh

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