Python SDK for sessh - a pure stdlib implementation with no external dependencies.
pip install -e .Or from source:
git clone https://github.com/CommandAGI/sessh-python-sdk.git
cd sessh-python-sdk
pip install -e .- Python 3.8+
- The
sesshCLI must be installed and on your PATH
from sessh import SesshClient
# Create a client
client = SesshClient(
alias="agent",
host="ubuntu@203.0.113.10",
identity="~/.ssh/id_ed25519"
)
# Open a session
client.open()
# Run commands
client.run("python train.py")
# Get logs
logs = client.logs(400)
print(logs["output"])
# Check status
status = client.status()
print(f"Master: {status['master']}, Session: {status['session']}")
# Close session
client.close()Initialize a sessh client.
Parameters:
alias(str): Session alias namehost(str): SSH host (user@host)port(int, optional): SSH port (default: 22)sessh_bin(str, optional): Path to sessh binary (default: "sessh" from PATH)identity(str, optional): Path to SSH private keyproxyjump(str, optional): ProxyJump host (e.g., "bastionuser@bastion")
Open or ensure a persistent remote tmux session.
Send a command into the persistent tmux session.
Capture recent output from the tmux session.
Check whether the SSH controlmaster and tmux session exist.
Kill tmux session and close the controlmaster.
Attach to the tmux session interactively. Note: This will block and take over the terminal.
Send individual key events to the tmux session (no Enter key). Useful for interactive TUI programs like vim or nano.
Read the current pane state from the tmux session. Useful for reading the current state of interactive TUI programs.
# Install in development mode
pip install -e ".[dev]"
# Run tests
pytestComprehensive examples are available in the examples/ directory. Each example demonstrates launching infrastructure, using sessh to manage persistent sessions, and cleaning up resources.
from sessh import SesshClient
client = SesshClient(alias="local-test", host="user@localhost")
client.open()
client.run("echo 'Hello from sessh!'")
logs = client.logs(50)
print(logs["output"])
client.close()import os
from sessh import SesshClient
# Launch EC2 instance, get IP (example)
ip = "203.0.113.10"
client = SesshClient(alias="aws-agent", host=f"ubuntu@{ip}")
client.open()
client.run("python train.py")
logs = client.logs(400)
client.close()import os
from sessh import SesshClient
# Launch Lambda Labs instance, get IP (example)
ip = "203.0.113.10"
client = SesshClient(
alias="lambda-agent",
host=f"ubuntu@{ip}",
identity="~/.ssh/id_ed25519"
)
client.open()
client.run("pip install torch torchvision")
client.run("python train.py")
logs = client.logs(400)
print(logs["output"])
client.close()from sessh import SesshClient
client = SesshClient(alias="editor", host="ubuntu@host")
client.open()
# Launch vim
client.run("vim file.txt")
# Read current pane to see vim's state
pane = client.pane(30)
print(pane["output"])
# Send key sequences to navigate and edit
client.keys("i") # Enter insert mode
client.keys("Hello") # Type text
client.keys("Esc") # Exit insert mode
client.keys(":wq") # Save and quit
client.keys("Enter") # Confirm
# Read pane again to verify
pane = client.pane(30)
print(pane["output"])
client.close()All examples follow the same pattern:
- Launch infrastructure (instance/container)
- Wait for SSH to be ready
- Open sessh session
- Run commands (state persists between commands)
- Fetch logs
- Clean up resources
Example Files:
examples/local.py- Localhost/local VMexamples/docker.py- Docker containerexamples/aws.py- AWS EC2examples/gcp.py- Google Cloud Platformexamples/lambdalabs.py- Lambda Labs GPUexamples/azure.py- Microsoft Azureexamples/digitalocean.py- DigitalOceanexamples/docker_compose.py- Docker Compose
Running Examples:
# Local example
python examples/local.py localhost
# AWS example (requires AWS credentials)
export AWS_REGION=us-east-1
export AWS_KEY_NAME=my-key
python examples/aws.py
# Lambda Labs example (requires API key)
export LAMBDA_API_KEY=sk_live_...
export LAMBDA_SSH_KEY=my-ssh-key
python examples/lambdalabs.pyIntegration tests are available in tests/test_examples.py. These tests require real infrastructure and are skipped by default.
Running Integration Tests:
# Enable integration tests
export SESSH_INTEGRATION_TESTS=1
# For local tests
export SESSH_TEST_HOST=localhost
pytest tests/test_examples.py::TestLocalExample
# For cloud provider tests (requires credentials)
export AWS_REGION=us-east-1
pytest tests/test_examples.py::TestAWSExamplefrom sessh import SesshClient
import sys
client = SesshClient(alias="test", host="ubuntu@host")
try:
client.open()
client.run("some-command")
except RuntimeError as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
finally:
client.close()"sessh: command not found"
- Ensure
sesshCLI is installed and on PATH - Or set
sessh_binparameter:SesshClient(..., sessh_bin="/usr/local/bin/sessh")
RuntimeError on operations
- Check that
sesshCLI works from command line - Verify SSH key permissions and configuration
- Ensure remote host has tmux installed
JSON parsing errors
- The SDK automatically sets
SESSH_JSON=1 - Verify
sesshCLI supports JSON output
See CONTRIBUTING.md for development guidelines.
- sessh - Core CLI
- sessh-mcp - MCP server for Cursor
- sessh-typescript-sdk - TypeScript SDK
MIT License - see LICENSE file for details.