Repolode
pahaz

pahaz/sshtunnel

Python library and CLI for opening SSH tunnels that forward local ports to services on remote or private hosts via paramiko.

Stars
1,305
Forks
200
Open issues
71
Last commit
2 days ago

What is it?

sshtunnel lets Python code and the command line set up SSH port forwarding. It is useful when a service such as a web app or database is only reachable through an SSH port, or when a private server sits behind a gateway that exposes only SSH.

The main entry point is SSHTunnelForwarder, which can be used with start()/stop() or as a context manager via open_tunnel. Once started, the tunnel exposes a local_bind_port that the application connects to. Authentication supports passwords, private key files with optional passphrases, and keys from an SSH agent or ~/.ssh/config. Multi-hop tunnels can be chained by pointing one tunnel at the local port of another.

The project also ships a sshtunnel CLI, which takes SSH credentials, remote bind addresses and local bind addresses as arguments. It is pure Python and runs on Windows as well as Unix-like systems.

Key features

  • 01Forward local ports to remote services reachable only over SSH.
  • 02Start and stop tunnels programmatically or with a with-statement.
  • 03Support password, private key and SSH agent authentication.
  • 04Chain multiple tunnels to reach hosts behind a gateway.
  • 05Provide a command-line interface for quick forwarding.
  • 06Read hosts and settings from ~/.ssh/config.

Use cases

  • Connecting to a database or web service on a server whose only open port is SSH.
  • Reaching a private host through a bastion or jump server from test scripts.
  • Forwarding a local development database such as a Vagrant VM port from a script.
  • Automating tunnel setup in CI or data pipelines that need remote resources.

Quick start

  1. 1

    Install the package from PyPI

    pip install sshtunnel
  2. 2

    Open a tunnel from Python using SSHTunnelForwarder and read server.local_bind_port

In collections

Topics

FAQ

What is sshtunnel?
Python library and CLI for opening SSH tunnels that forward local ports to services on remote or private hosts via paramiko. sshtunnel lets Python code and the command line set up SSH port forwarding. It is useful when a service such as a web app or database is only reachable through an SSH port, or when a private server sits behind a gateway that exposes only SSH.
Is sshtunnel free to use? What license does it have?
sshtunnel is open source under the MIT license. Check the license terms before using it in commercial products.
Is sshtunnel still maintained?
Actively maintained. The last commit to sshtunnel was on Nov 6, 2026.
What can I use sshtunnel for?
Connecting to a database or web service on a server whose only open port is SSH. Reaching a private host through a bastion or jump server from test scripts. Forwarding a local development database such as a Vagrant VM port from a script. Automating tunnel setup in CI or data pipelines that need remote resources.
How do I install sshtunnel?
Install the package from PyPI: pip install sshtunnel Open a tunnel from Python using SSHTunnelForwarder and read server.local_bind_port
Where can I get sshtunnel?
The source code and installation instructions are on GitHub: https://github.com/pahaz/sshtunnel

Summary generated by AI from the project's README. GitHub stats refresh daily.

Related projects