Skip to main content

Auto-forward SSH ports

Project description

SSH Auto Port Forwarder

Automatically detect and forward ports from a remote SSH server to your local machine. Similar to VS Code's port forwarding feature, but fully automatic.

Features

  • Automatically discovers listening ports on the remote server
  • Shows process names for each forwarded port
  • Forwards ports to your local machine via SSH tunneling
  • Handles port conflicts by finding alternative local ports
  • Auto-detects new ports and starts forwarding
  • Auto-detects closed ports and stops forwarding
  • Terminal title shows tunnel count
  • Runs in the background with status updates
  • Reads connection details from your SSH config
  • Skips well-known ports (< 1000) by default

Installation

With uv (recommended):

uvx ssh-auto-forward hetzner

Install locally:

cd portforwards
uv sync

This installs the ssh-auto-forward command.

Local development:

make run ARGS=hetzner
make run ARGS="hetzner -v"

Usage

Basic usage - uses host from your SSH config:

ssh-auto-forward hetzner

Options:

-v, --verbose           Enable verbose logging
-i, --interval SECS     Scan interval in seconds (default: 5)
-p, --port-range MIN:MAX Local port range for remapping (default: 3000:10000)
-s, --skip PORTS        Comma-separated ports to skip (default: all ports < 1000)
-c, --config PATH       Path to SSH config file
--version               Show version and exit

Examples:

# Scan every 3 seconds
ssh-auto-forward hetzner -i 3

# Use specific port range
ssh-auto-forward hetzner -p 4000:9000

# Skip specific ports
ssh-auto-forward hetzner -s 22,80,443

# Verbose mode
ssh-auto-forward hetzner -v

How it works

  1. Connects to your remote server using your SSH config
  2. Runs ss -tlnp on the remote to find listening ports
  3. Creates SSH tunnels for each discovered port
  4. Continuously monitors for new/closed ports
  5. Handles port conflicts on your local machine

Status messages

✓ Connected!
✓ Forwarding port 2999 (python3)
✓ Forwarding port 7681 (ttyd)
✓ Forwarding remote port 19840 -> local port 3000 (node)
✗ Remote port 2999 is no longer listening, stopping tunnel

The terminal title also updates to show: ssh-auto-forward: hetzner (18 tunnels active)

Testing

Start a test server on your remote machine:

ssh hetzner "python3 -m http.server 9999 --bind 127.0.0.1 &"

Then run ssh-auto-forward hetzner and you should see:

✓ Forwarding remote port 9999 -> local port 3003 (python3)

Access it locally:

curl http://localhost:3003/

Stopping

Press Ctrl+C to stop the forwarder and close all tunnels.

Requirements

  • Python 3.10+
  • paramiko
  • Remote server must have ss or netstat command available

Tests

Unit tests (run locally, no SSH required):

make test
# or
uv run pytest tests/ -v

Integration tests (require SSH access):

SSH_AUTO_FORWARD_TEST_HOST=hetzner uv run pytest tests_integration/ -v

The integration tests:

  • Test that remote ports are forwarded to the same local port when available
  • Test that ports increment by 1 when the local port is busy
  • Test auto-detection of new ports
  • Test auto-cleanup when remote ports close

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

ssh_auto_forward-0.0.1.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

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

ssh_auto_forward-0.0.1-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ssh_auto_forward-0.0.1.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.3 cpython/3.10.19 HTTPX/0.28.1

File hashes

Hashes for ssh_auto_forward-0.0.1.tar.gz
Algorithm Hash digest
SHA256 0bd0e1ebe9263a9905775224122063539a05bee2d20038e68fc5ee5e301124a4
MD5 0047c3b606f1e80b0966c3b85fb8fdf6
BLAKE2b-256 e0a91f918851070c648cda58174692a23a1d7a9bb035eaadbe923c754baa32f9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssh_auto_forward-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.3 cpython/3.10.19 HTTPX/0.28.1

File hashes

Hashes for ssh_auto_forward-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fd71d1303ea9dcf0f6906b8ab1e541790432942e1234a51bcbb0cc857b2d5122
MD5 ef5f293dbfec4f3314004888aaaba49d
BLAKE2b-256 a83994e21adf7413f9178835f291b774b1d0af3b5ceffc726d6405411bf7b0db

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