Persistent SSH reverse tunnel for SciTeX (NAT traversal)
Project description
SciTeX Tunnel (scitex-tunnel)
Persistent SSH (Secure Shell) reverse tunnel for NAT (Network Address Translation) traversal
Full Documentation · pip install scitex-tunnel
Problem
Machines behind NAT (Network Address Translation) or institutional firewalls cannot receive incoming SSH (Secure Shell) connections. Researchers running long experiments on lab workstations, HPC (High-Performance Computing) nodes, or edge devices need reliable remote access without manual port forwarding or VPN (Virtual Private Network) setup. Existing solutions (ngrok, cloudflared) often require external accounts or lack systemd integration for persistent, auto-recovering connections.
Solution
SciTeX Tunnel creates persistent reverse SSH tunnels using autossh and systemd. Each tunnel runs as a managed service that auto-restarts on failure, survives reboots, and requires only a bastion server with SSH access.
┌─────────────────────────────────────────┐ ┌──────────────────────┐ ┌──────────────────┐
│ Lab Workstation (behind NAT/firewall) │ │ Bastion Server │ │ Remote Client │
│ │ │ (public IP) │ │ (laptop, etc.) │
│ ┌──────────────────────────────────┐ │ │ │ │ │
│ │ systemd service │ │ │ │ │ │
│ │ autossh-tunnel-{port}.service │ │ │ │ │ │
│ │ ┌──────────────────────────┐ │ │ │ ┌────────────────┐ │ │ │
│ │ │ autossh │ │ │ │ │ sshd listening │ │ │ │
│ │ │ (auto-reconnect daemon) │───┼───┼─────┼──│ on port {port} │──┼─────│ ssh -p {port} │
│ │ │ │ │ │ │ │ │ │ │ bastion-server │
│ │ └──────────────────────────┘ │ │ │ └────────────────┘ │ │ │
│ └──────────────────────────────────┘ │ │ │ │ │
│ │ │ │ │ │
│ localhost:22 (SSH server) │ │ │ │ │
└─────────────────────────────────────────┘ └──────────────────────┘ └──────────────────┘
────── reverse tunnel ──────► ◄─── SSH connection ───
-R {port}:localhost:22 bastion-server ssh -p {port} bastion-server
Figure 1. Architecture overview. The lab workstation initiates a reverse SSH tunnel to the bastion server. The remote client connects to the bastion server, which forwards the connection back through the tunnel to the lab workstation.
How It Works
setup(requires sudo) writes a systemd unit file at/etc/systemd/system/autossh-tunnel-{port}.servicethat runs autossh with the reverse tunnel flag (-R {port}:localhost:22). The service is enabled (starts on boot) and started immediately.- autossh monitors the SSH connection and automatically re-establishes it if the connection drops — network interruptions, server reboots, or SSH timeouts are handled transparently.
- systemd ensures the service survives host reboots (
WantedBy=multi-user.target) and restarts on process failure (Restart=always,RestartSec=3). - A remote client connects to the bastion server on the forwarded port, and the connection is routed back through the tunnel to the lab workstation's SSH server (port 22).
| Operation | What it does |
|---|---|
| setup | Creates a systemd service at /etc/systemd/system/autossh-tunnel-{port}.service that maintains a reverse SSH tunnel via autossh |
| status | Queries systemd for tunnel service state (systemctl status) |
| remove | Stops, disables, and deletes the systemd service file |
Table 1. Three operations. Each maps to a CLI (Command-Line Interface) command, Python function, and MCP (Model Context Protocol) tool.
Installation
Requires autossh on the host machine (sudo apt install autossh).
pip install scitex-tunnel
SciTeX users:
pip install scitexalready includes tunnel support.
Note:
setupandremoverequire sudo privileges because they write systemd service files to/etc/systemd/system/and runsystemctlcommands. You will be prompted for your password.
Disclaimer: Before setting up reverse tunnels, please check your organization's acceptable use policy and network terms of service. Reverse tunnels may bypass institutional firewalls or network policies. The authors accept no responsibility for any consequences arising from the use of this software.
Alternative: No-sudo setup via ~/.bashrc (no root access needed)
If you do not have sudo access (e.g., shared HPC nodes, university servers), you can run autossh directly from your shell profile. Add to ~/.bashrc:
# Persistent reverse tunnel without sudo — starts on every login
# Checks if tunnel is already running before starting
if ! pgrep -f "autossh.*-R 2222:localhost:22" > /dev/null 2>&1; then
autossh -M 0 -f -N \
-o "PubkeyAuthentication=yes" \
-o "PasswordAuthentication=no" \
-o "ServerAliveInterval=30" \
-o "ServerAliveCountMax=3" \
-i ~/.ssh/id_rsa \
-R 2222:localhost:22 user@bastion.example.com
fi
Trade-offs vs. systemd approach:
- No sudo required
- Starts on user login (not on boot — requires an active login session)
- No automatic restart if autossh crashes between logins
-fruns autossh in the background;-M 0relies on SSH keepalives
Alternative: Persistent session via screen, tmux, or nohup (no root, survives logout)
For long-running sessions on HPC or shared servers where you want the tunnel to survive logout:
# Option 1: screen (detaches from terminal)
screen -dmS tunnel autossh -M 0 -N \
-o "ServerAliveInterval=30" -o "ServerAliveCountMax=3" \
-i ~/.ssh/id_rsa -R 2222:localhost:22 user@bastion.example.com
# Reattach: screen -r tunnel
# Kill: screen -S tunnel -X quit
# Option 2: tmux
tmux new-session -d -s tunnel "autossh -M 0 -N \
-o ServerAliveInterval=30 -o ServerAliveCountMax=3 \
-i ~/.ssh/id_rsa -R 2222:localhost:22 user@bastion.example.com"
# Reattach: tmux attach -t tunnel
# Kill: tmux kill-session -t tunnel
# Option 3: nohup (simplest, no terminal multiplexer needed)
nohup autossh -M 0 -N \
-o "ServerAliveInterval=30" -o "ServerAliveCountMax=3" \
-i ~/.ssh/id_rsa -R 2222:localhost:22 user@bastion.example.com \
> /dev/null 2>&1 &
# Kill: pkill -f "autossh.*-R 2222:localhost:22"
Trade-offs: No sudo needed. Survives logout (unlike ~/.bashrc approach). Does not survive reboot — you must restart manually or add the command to a cron @reboot job.
Alternative: Direct shell scripts (no Python required)
If you have sudo access but prefer not to install Python, use the shell scripts directly:
# Download the scripts (one-time)
curl -o ~/.local/bin/setup-autossh-service.sh \
https://raw.githubusercontent.com/ywatanabe1989/scitex-tunnel/main/src/scitex_tunnel/scripts/setup-autossh-service.sh
curl -o ~/.local/bin/remove-autossh-service.sh \
https://raw.githubusercontent.com/ywatanabe1989/scitex-tunnel/main/src/scitex_tunnel/scripts/remove-autossh-service.sh
chmod +x ~/.local/bin/setup-autossh-service.sh ~/.local/bin/remove-autossh-service.sh
# Usage (requires sudo)
setup-autossh-service.sh -p 2222 -b user@bastion.example.com -s ~/.ssh/id_rsa
remove-autossh-service.sh -p 2222
Quick Start
# Set up a persistent reverse tunnel
scitex-tunnel setup -p 2222 -b user@bastion.example.com -s ~/.ssh/id_rsa
# Check tunnel status
scitex-tunnel status
# Remove a tunnel
scitex-tunnel remove -p 2222
Three Interfaces
Python API (Application Programming Interface)
import scitex_tunnel
# Set up tunnel
result = scitex_tunnel.setup(2222, "user@bastion.example.com", "~/.ssh/id_rsa")
# Check status
result = scitex_tunnel.status()
result = scitex_tunnel.status(port=2222)
# Remove tunnel
result = scitex_tunnel.remove(2222)
CLI Commands
scitex-tunnel --help-recursive # Show all commands
scitex-tunnel setup -p 2222 -b user@host -s ~/.ssh/id_rsa
scitex-tunnel status # All tunnels
scitex-tunnel status -p 2222 # Specific port
scitex-tunnel remove -p 2222 # Remove tunnel
scitex-tunnel list-python-apis # List Python APIs
scitex-tunnel mcp list-tools # List MCP (Model Context Protocol) tools
MCP (Model Context Protocol) Server — for AI Agents
AI agents can manage tunnels autonomously.
| Tool | Description |
|---|---|
tunnel_setup |
Set up a persistent SSH reverse tunnel |
tunnel_status |
Check status of SSH reverse tunnels |
tunnel_remove |
Remove a persistent SSH reverse tunnel |
Table 2. Three MCP tools. All tools accept JSON (JavaScript Object Notation) parameters and return JSON results.
scitex-tunnel mcp start
Environment Variables
| Variable | Description | Example |
|---|---|---|
SCITEX_TUNNEL_BASTION_SERVER |
Default bastion server | user@bastion.example.com |
SCITEX_TUNNEL_SECRET_KEY_PATH |
Default SSH (Secure Shell) private key path | ~/.ssh/id_rsa |
SCITEX_TUNNEL_DEBUG_MODE |
Enable verbose output (1) |
0 |
Table 3. Environment variables. CLI flags take precedence when provided.
Set these in .env or your shell profile to avoid repeating -b and -s flags:
export SCITEX_TUNNEL_BASTION_SERVER=user@bastion.example.com
export SCITEX_TUNNEL_SECRET_KEY_PATH=~/.ssh/id_rsa
# Now just specify the port
scitex-tunnel setup -p 2222
Part of SciTeX
Tunnel is part of SciTeX. When used inside the SciTeX framework, tunnel management integrates with the orchestrator:
import scitex
# Manage tunnels through the unified interface
result = scitex.tunnel.setup(2222, "user@bastion.example.com", "~/.ssh/id_rsa")
scitex.tunnel.status()
The SciTeX system follows the Four Freedoms for Research below, inspired by the Free Software Definition:
Four Freedoms for Research
- The freedom to run your research anywhere — your machine, your terms.
- The freedom to study how every step works — from raw data to final manuscript.
- The freedom to redistribute your workflows, not just your papers.
- The freedom to modify any module and share improvements with the community.
AGPL-3.0 — because we believe research infrastructure deserves the same freedoms as the software it runs on.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file scitex_tunnel-0.2.3.tar.gz.
File metadata
- Download URL: scitex_tunnel-0.2.3.tar.gz
- Upload date:
- Size: 405.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9f1867d1e3f4991d16250d8a5e5f4675780a9eb151e8d460c7985a44d4102c46
|
|
| MD5 |
f7d00e304275dfced5a4ef97cd8aa9a1
|
|
| BLAKE2b-256 |
cab677743d2f9cd70d2184d1297a90eed9d777210645ecf5f37db2764bb01aa5
|
Provenance
The following attestation bundles were made for scitex_tunnel-0.2.3.tar.gz:
Publisher:
publish-pypi.yml on ywatanabe1989/scitex-tunnel
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
scitex_tunnel-0.2.3.tar.gz -
Subject digest:
9f1867d1e3f4991d16250d8a5e5f4675780a9eb151e8d460c7985a44d4102c46 - Sigstore transparency entry: 1155182348
- Sigstore integration time:
-
Permalink:
ywatanabe1989/scitex-tunnel@7fecb4579142b0d39453de1b6988940e2298bde3 -
Branch / Tag:
refs/tags/v0.2.3 - Owner: https://github.com/ywatanabe1989
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@7fecb4579142b0d39453de1b6988940e2298bde3 -
Trigger Event:
push
-
Statement type:
File details
Details for the file scitex_tunnel-0.2.3-py3-none-any.whl.
File metadata
- Download URL: scitex_tunnel-0.2.3-py3-none-any.whl
- Upload date:
- Size: 16.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2d3f3e580c3d180cecdf12c00c1d7fd187c992a9ec2dd3a346dcd3cdfddf02bd
|
|
| MD5 |
f21b864b11922f5695b91a0bff6d857a
|
|
| BLAKE2b-256 |
1cb421a1db82decfda3cc5f61a00abc41d148e1db3cfa898e97b0370e7656cbd
|
Provenance
The following attestation bundles were made for scitex_tunnel-0.2.3-py3-none-any.whl:
Publisher:
publish-pypi.yml on ywatanabe1989/scitex-tunnel
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
scitex_tunnel-0.2.3-py3-none-any.whl -
Subject digest:
2d3f3e580c3d180cecdf12c00c1d7fd187c992a9ec2dd3a346dcd3cdfddf02bd - Sigstore transparency entry: 1155182349
- Sigstore integration time:
-
Permalink:
ywatanabe1989/scitex-tunnel@7fecb4579142b0d39453de1b6988940e2298bde3 -
Branch / Tag:
refs/tags/v0.2.3 - Owner: https://github.com/ywatanabe1989
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@7fecb4579142b0d39453de1b6988940e2298bde3 -
Trigger Event:
push
-
Statement type: