
Tunnel SSH verso server remoto.
|Test| |AppVeyor| |readthedocs| |coveralls| |versione|
|versioni py| |licenza|
Autore: Pahaz_
Repo: https://github.com/pahaz/sshtunnel/
Ispirato a https://github.com/jmagnusson/bgtunnel, che non funziona su Windows.
Vedi anche: https://github.com/paramiko/paramiko/blob/master/demos/forward.py
paramiko_sshtunnel_ è su PyPI, quindi basta eseguire:
::
pip install sshtunnel
oppure ::
easy_install sshtunnel
oppure ::
conda install -c conda-forge sshtunnel
per installarlo nel proprio ambiente.
Per installare dal sorgente, clonare il
repo <https://github.com/pahaz/sshtunnel>_ ed eseguire::
python setup.py install
Per eseguire i test è prima necessario
tox <https://testrun.org/tox/latest/>_ ed eseguire::
python setup.py test
Uno degli scenari tipici in cui sshtunnel è utile è illustrato nella
figura seguente. L'utente potrebbe aver bisogno di connettersi a una porta di un server remoto (ad es. 8080)
dove è raggiungibile solo la porta SSH (solitamente la porta 22). ::
----------------------------------------------------------------------
|
-------------+ | +----------+
LOCAL | | | REMOTE | :22 SSH
CLIENT | <== SSH ========> | SERVER | :8080 web service
-------------+ | +----------+
|
FIREWALL (only port 22 is open)
----------------------------------------------------------------------
Fig1: Come connettersi a un servizio bloccato da un firewall tramite un tunnel SSH.
Se consentito dal server SSH, è anche possibile raggiungere un server privato
(dal punto di vista di REMOTE SERVER) non direttamente visibile dall'esterno
(dal punto di vista di LOCAL CLIENT). ::
----------------------------------------------------------------------
|
-------------+ | +----------+ +---------
LOCAL | | | REMOTE | | PRIVATE
CLIENT | <== SSH ========> | SERVER | <== local ==> | SERVER
-------------+ | +----------+ +---------
|
FIREWALL (only port 443 is open)
----------------------------------------------------------------------
Fig2: Come connettersi a PRIVATE SERVER tramite un tunnel SSH.
L'API consente sia di inizializzare il tunnel e avviarlo, sia di utilizzare un contesto with,
che si occuperà di avviare e fermare il tunnel:
Di seguito il codice corrispondente alla Fig1 sopra, dato che l'indirizzo del server remoto è
pahaz.urfuclub.ru, con autenticazione tramite password e porta di bind locale assegnata casualmente.
.. code-block:: python
from sshtunnel import SSHTunnelForwarder
server = SSHTunnelForwarder(
'alfa.8iq.dev',
ssh_username="pahaz",
ssh_password="secret",
remote_bind_address=('127.0.0.1', 8080)
)
server.start()
print(server.local_bind_port) # show assigned local port
# work with `SECRET SERVICE` through `server.local_bind_port`.
server.stop()
Esempio di port forwarding verso un server privato non direttamente raggiungibile, assumendo un'autenticazione pkey protetta da password, il servizio SSH del server remoto in ascolto sulla porta 443 e che tale porta sia aperta nel firewall (Fig2):
.. code-block:: python
import paramiko
import sshtunnel
with sshtunnel.open_tunnel(
(REMOTE_SERVER_IP, 443),
ssh_username="",
ssh_pkey="/var/ssh/rsa_key",
ssh_private_key_password="secret",
remote_bind_address=(PRIVATE_SERVER_IP, 22),
local_bind_address=('0.0.0.0', 10022)
) as tunnel:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('127.0.0.1', 10022)
# do some operations with client session
client.close()
print('FINISH!')
Esempio di port forwarding per la porta locale MySQL di Vagrant:
.. code-block:: python
from sshtunnel import open_tunnel
from time import sleep
with open_tunnel(
('localhost', 2222),
ssh_username="vagrant",
ssh_password="vagrant",
remote_bind_address=('127.0.0.1', 3306)
) as server:
print(server.local_bind_port)
while True:
# press Ctrl-C for stopping
sleep(1)
print('FINISH!')
Oppure semplicemente usando la CLI:
.. code-block:: console
(bash)$ python -m sshtunnel -U vagrant -P vagrant -L :3306 -R 127.0.0.1:3306 -p 2222 localhost
Apertura di una sessione SSH passando attraverso due tunnel. Il trasporto SSH e i tunnel verranno eseguiti come daemon, il che non attenderà la chiusura delle connessioni al momento della chiusura.
.. code-block:: python
import sshtunnel
from paramiko import SSHClient
with sshtunnel.open_tunnel(
ssh_address_or_host=('GW1_ip', 20022),
remote_bind_address=('GW2_ip', 22),
) as tunnel1:
print('Connection to tunnel1 (GW1_ip:GW1_port) OK...')
with sshtunnel.open_tunnel(
ssh_address_or_host=('localhost', tunnel1.local_bind_port),
remote_bind_address=('target_ip', 22),
ssh_username='GW2_user',
ssh_password='GW2_pwd',
) as tunnel2:
print('Connection to tunnel2 (GW2_ip:GW2_port) OK...')
with SSHClient() as ssh:
ssh.connect('localhost',
port=tunnel2.local_bind_port,
username='target_user',
password='target_pwd',
)
ssh.exec_command(...)
::
$ sshtunnel --help
usage: sshtunnel [-h] [-U SSH_USERNAME] [-p SSH_PORT] [-P SSH_PASSWORD] -R
IP:PORT [IP:PORT ...] [-L [IP:PORT ...]] [-k SSH_HOST_KEY]
[-K KEY_FILE] [-S KEY_PASSWORD] [-t] [-v] [-V] [-x IP:PORT]
[-c SSH_CONFIG_FILE] [-z] [-n] [-d [FOLDER ...]]
ssh_address
Pure python ssh tunnel utils
Version 0.5.1
positional arguments:
ssh_address SSH server IP address (GW for SSH tunnels)
set with "-- ssh_address" if immediately after -R or -L