Skip to main content

agent-pty

PyPI

Persistent SSH sessions with a full interactive PTY, held open in the background.

Open a session once. Then run commands, attach to the live shell, forward ports, or move files from separate terminal invocations. Commands share one shell, so cd, exports and functions persist between them. The session survives until you close it or its idle TTL expires.

Install

uv tool install agent-pty            # from PyPI
uv tool install --from . agent-pty   # local checkout
uv sync && uv run agent-pty --help   # development

Package page: https://pypi.org/project/agent-pty/

Python 3.11 or newer.

Quick start

# 1. open a background session. HOST resolves through ~/.ssh/config
agent-pty connect web --name web --ttl 30

# 2. use it from anywhere
agent-pty exec -s web 'cd /srv/app && git pull'
agent-pty exec -s web 'pwd'                   # /srv/app, state kept
agent-pty shell  -s web                       # full PTY, Ctrl-] detaches
agent-pty logs   -s web -n 50                 # PTY transcript
agent-pty forward -s web -L 8080:localhost:80 # local tunnel to the remote port
agent-pty forward -s web -D 1080              # SOCKS5 proxy through the session
agent-pty connect web -J bastion             # reach web through a jump host
agent-pty upload ./build.tar.gz '~/releases/'
agent-pty sessions
agent-pty close  -s web

-s accepts a session id, its name, or a unique id prefix. It can be omitted when exactly one session is live.

Commands

Command Purpose
connect HOST Open a background session with an interactive PTY.
sessions List live sessions: id, name, target, pid, idle, start, TTL.
info Show one session: host, user, shell, idle, uptime, TTL, connected, running, forwards, transcript.
exec CMD... Run a command inside the live PTY shell. State persists across calls.
shell Attach to the live PTY. Ctrl-] detaches without closing the session.
send TEXT Type text into the live PTY shell.
interrupt Send Ctrl-C, and stop a running exec with exit code 130.
logs Print the session transcript, with -f to follow it.
rename NEWNAME Give the session a new name.
forward Add, list, or remove SSH port forwards, including a SOCKS5 proxy.
upload FILE [REMOTE] Upload one file. A trailing / on REMOTE means that directory.
upload-dir DIR [REMOTE] Upload a directory tree.
download FILE [LOCAL] Download one file. A trailing / on LOCAL means that directory.
download-dir DIR [LOCAL] Download a directory tree.
close Close a session and its SSH connection. --all closes every session.
prune Delete files left by closed or crashed sessions.

Flags

Flag Commands Meaning
-p, --port connect SSH port. Default 22 or ~/.ssh/config.
-u, --user connect Remote user. Default from ~/.ssh/config, $USER, or root.
-i, --key-file connect Private key file. Default from ~/.ssh/config IdentityFile.
--password connect SSH password. Prompted when neither password nor key is given.
--ttl connect Idle timeout in minutes, default 30.
-n, --name connect Friendly session name, usable with -s.
--insecure connect Do not verify or record host keys.
--agent / --no-agent connect Use ssh-agent keys, on by default.
-J, --jump connect [USER@]HOST[:PORT], repeatable for a chain.
--jump-password connect Password for a jump host, repeatable in -J order.
--forward-agent connect Forward the local ssh-agent to the remote side.
--reconnect connect Reconnect attempts after a dropped connection, default 0.
--connect-timeout connect SSH connect timeout in seconds, default 20.
--keepalive connect SSH keepalive interval in seconds, default 30, 0 disables.
-s, --session most commands Session id, name, or unique id prefix.
--timeout exec Seconds to wait, default 120. On expiry the command is interrupted and agent-pty exits 124.
--no-newline send Type the text without a trailing newline.
-n, --lines logs Lines to print, default 40.
-f, --follow logs Keep printing new output until Ctrl-C.
--raw logs Keep terminal escape sequences.
-L, --local forward [BIND:]PORT:HOST:PORT, listen locally. Repeatable.
-R, --remote forward [BIND:]PORT:HOST:PORT, listen on the remote side. Repeatable.
-D, --dynamic forward [BIND:]PORT, listen locally as a SOCKS5 proxy. Repeatable.
--list forward Show active forwards.
--remove ID forward Remove a forward by id.
--json sessions, info Machine-readable output.
--all close Close every live session.
-h, --help anywhere Full help, including every subcommand and flag.

Port forwarding

agent-pty forward -s web -L 8080:localhost:80      # local 8080 -> web's port 80
agent-pty forward -s web -L 15432:db:5432          # local 15432 -> web -> db:5432
agent-pty forward -s web -R 9000:localhost:3000    # web's 9000 -> your 3000
agent-pty forward -s web -D 1080                   # SOCKS5, resolves from the remote side
agent-pty forward -s web --list
agent-pty forward -s web --remove 1

For -L and -D, HOST:PORT is resolved on the remote side. For -R, it is resolved on your side. All default to binding 127.0.0.1. IPv6 literals use brackets, for example -L [::1]:8080:localhost:80.

curl --socks5-hostname 127.0.0.1:1080 http://internal.service/

Behavior

  • One daemon process per session, under ~/.agent-pty/sessions/ (mode 0700). The daemon dies with its session.
  • HOST resolves through ~/.ssh/config for HostName, User, Port and the first IdentityFile.
  • exec sends one compound command to the interactive shell and brackets it with control-byte markers, so prompts, echoed input and shell hooks stay out of the captured output. Since a PTY merges streams, stderr appears with stdout. The remote exit code becomes agent-pty's exit code.
  • exec and shell are mutually exclusive on one session: attaching while a command runs is refused, and vice versa.
  • shell is a real PTY. Window size is applied on attach and followed on every resize.
  • Host keys use trust on first use: keys come from ~/.ssh/known_hosts and ~/.agent-pty/known_hosts. A changed key fails the connection; remove the stale entry there after a legitimate rotation. --insecure skips this.
  • The full PTY stream is appended to ~/.agent-pty/sessions/<id>.out and read by logs.
  • Every operation is appended to ~/.agent-pty/audit.jsonl: timestamp, session id, action, command, exit code, transfer paths, forwards. Passwords are never written there.
  • Jump hosts are honoured from -J, or from ProxyJump in ~/.ssh/config. Chains of any length work, and each hop uses its own ssh config or SSH agent.
  • --forward-agent requests agent forwarding before the shell starts, so the remote shell gets SSH_AUTH_SOCK. It needs SSH_AUTH_SOCK locally.
  • --reconnect N retries a dropped connection with backoff. With a zsh or bash login shell it also restores exported variables, aliases and functions, plus the last working directory, and re-creates port forwards. Running processes and history are not restored. Other shells get the directory and forwards.
  • Reconnect state is captured after each exec, which adds a small delay per command. Sessions without --reconnect skip the capture.
  • Each --jump-password applies to the matching -J hop; pass one per hop, or none to use ssh config keys or the agent.
  • The idle TTL closes abandoned sessions and their forwards.
  • Remote ~ is expanded for upload and download targets.
  • upload creates missing remote parent directories and copies directories without needing upload-dir. Symlinked directories are skipped to avoid loops.
  • download detects a remote directory and copies the tree without needing download-dir.
  • sessions --json adds live fields: idle, uptime, attached, running and forwards. logs -f stops when the session ends.
  • exec framing expects a POSIX shell (bash, zsh or sh).
  • info reports the detected shell and the size of the captured reconnect state.

Layout

src/agent_pty/main.py      CLI and client transport
src/agent_pty/session.py   SSH session, PTY pump, exec framing, forwarding, daemon
tests/test_cli.py          help surface, exec framing, forwards, transcript cleaning

Releases

Publishing a GitHub release runs the Publish workflow, which builds the wheel and sdist and uploads them to PyPI with the PYPI_TOKEN repository secret. A manual run is available from the Actions tab.

Development

uv sync --all-groups
uv run ruff check . && uv run ruff format --check .
uv run pytest -q

Metadata

Release files for agent-pty 0.7.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for agent-pty 0.7.1
File Size Uploaded Provenance Actions
agent_pty-0.7.1.tar.gz 24.0 kB Without provenance Details for agent_pty-0.7.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for agent-pty 0.7.1
File Interpreter ABI Platform Actions
agent_pty-0.7.1-py3-none-any.whl Python 3 none any Details for agent_pty-0.7.1-py3-none-any.whl

Total release size: 49.3 kB

Release files / agent_pty-0.7.1.tar.gz

Download URL agent_pty-0.7.1.tar.gz
Size 24.0 kB
Tags Source
SHA-256 checksum
How to use checksums
9ca4f7f577e427ad6341db3d4a8a6bd06efe1c3605d6fa98a07bacc82dd220a7
BLAKE2b-256 checksum
How to use checksums
28bcc5634bc3b85fa0b1844246067ebb309090c994974ba8bbd0773bb8cbfdd2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / agent_pty-0.7.1-py3-none-any.whl

Download URL agent_pty-0.7.1-py3-none-any.whl
Size 25.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ad5ec8e3075c45af94fcf361dbb26367153be6842058d7fb7348abef52e00772
BLAKE2b-256 checksum
How to use checksums
69c3cd6d6d22ff625490eef67c7bf51a5452b689aecab8253103eabfbf0fd706
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for agent-pty 0.7.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.7.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page