Skip to main content

Initiate SSH tunnels

Project description

**Author**: **[Pahaz Blinov](**


Inspired by but it doesn't work on Windows.
See also:

Require `paramiko`.

# Install #

pip install sshtunnel


easy_install sshtunnel

# SSH tunnels to remote server #

Useful when you need to connect to local port on remote server throw ssh
tunnel. It works by opening a port forwarding ssh connection in the
background, using threads. The connection(s) are closed when explicitly
calling the `close` method of the returned SSHTunnelForwarder object.


-------------+ | +----------+ +---------
SERVER | <== SSH ========> | SERVER | <== local ==> | SERVER
-------------+ | +----------+ +---------


Fig1: How to connect to PRIVATE SERVER throw SSH tunnel.

## Ex 1: ##

from sshtunnel import SSHTunnelForwarder

server = SSHTunnelForwarder(
('', 22),
remote_bind_address=('', 5555))


# work with `SECRET SERVICE` throw `server.local_bind_port`.


# Ex 2: ##

Example of a port forwarding for the Vagrant MySQL local port:

from sshtunnel import SSHTunnelForwarder
from time import sleep

with SSHTunnelForwarder(
('localhost', 2222),
remote_bind_address=('', 3306)) as server:

while True:
# press Ctrl-C for stopping


Or simple use CLI:

python -m sshtunnel -U vagrant -P vagrant -L :3306 -R -p 2222 localhost


- [Cameron Maske](
- [Gustavo Machado](
- [Colin Jermain](
- [J.M. Fernández]( - (big thanks!)
- [Lewis Thompson](
- [Erik Rogers](

# TODO #

- Write tests!


## work in progress ##

## v. ##
- fix Thread.daemon mode for Python < 3.3 (lewisthompson, ewrogers)

## v. ##
- fix CLI issues/13 (pahaz)

## v.0.0.4 ##
- daemon mode by default for all threads (fernandezcuesta, pahaz) - *incompatible*
- move `make_ssh_forward_server` to `SSHTunnelForwarder.make_ssh_forward_server` (pahaz, fernandezcuesta) - *incompatible*
- move `make_ssh_forward_handler` to `SSHTunnelForwarder.make_ssh_forward_handler_class` (pahaz, fernandezcuesta) - *incompatible*
- rename `open` to `open_tunnel` (fernandezcuesta) - *incompatible*
- add CLI interface (fernandezcuesta)
- support opening several tunnels at once (fernandezcuesta)
- improve stability and readability (fernandezcuesta, pahaz)
- improve logging (fernandezcuesta, pahaz)
- add `raise_exception_if_any_forwarder_have_a_problem` argument for opening several tunnels at once (pahaz)
- add `ssh_config_file` argument support (fernandezcuesta)
- add Python 3 support (fernandezcuesta, pahaz)

## v.0.0.3 ##
- add `threaded` options (cameronmaske)
- fix exception error message, correctly printing destination address (gdmachado)
- fix pip install fails (cjermain, pahaz)

## v.0.0.1 ##
- `SSHTunnelForwarder` class (pahaz)
- `open` function (pahaz)

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for sshtunnel, version
Filename, size File type Python version Upload date Hashes
Filename, size (17.7 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page