
|テスト| |AppVeyor| |readthedocs| |coveralls| |バージョン|
|pyversions| |ライセンス|
作者: Pahaz_
リポジトリ: https://github.com/pahaz/sshtunnel/
https://github.com/jmagnusson/bgtunnel に触発されたものですが、こちらは Windows では動作しません。
関連情報: https://github.com/paramiko/paramiko/blob/master/demos/forward.py
paramiko_sshtunnel_ は PyPI にあるので、単純に以下を実行します:
::
pip install sshtunnel
または ::
easy_install sshtunnel
または ::
conda install -c conda-forge sshtunnel
これで環境にインストールされます。
ソースからインストールするには、
リポジトリ <https://github.com/pahaz/sshtunnel>_ をクローンして以下を実行します::
python setup.py install
テストを実行するには、まず
tox <https://testrun.org/tox/latest/>_ が必要です。そして以下を実行します::
python setup.py test
sshtunnel が役立つ典型的なシナリオの一つを下図に示します。ユーザーは、SSH ポート(通常はポート 22)のみが到達可能なリモートサーバーのポート(例: 8080)に接続する必要がある場合があります。 ::
----------------------------------------------------------------------
|
-------------+ | +----------+
LOCAL | | | REMOTE | :22 SSH
CLIENT | <== SSH ========> | SERVER | :8080 web service
-------------+ | +----------+
|
FIREWALL (only port 22 is open)
----------------------------------------------------------------------
図1: ファイアウォールでブロックされたサービスに SSH トンネル経由で接続する方法。
SSH サーバーが許可している場合、(REMOTE SERVER から見て)外部(LOCAL CLIENT から見て)から直接見えないプライベートサーバーに到達することも可能です。 ::
----------------------------------------------------------------------
|
-------------+ | +----------+ +---------
LOCAL | | | REMOTE | | PRIVATE
CLIENT | <== SSH ========> | SERVER | <== local ==> | SERVER
-------------+ | +----------+ +---------
|
FIREWALL (only port 443 is open)
----------------------------------------------------------------------
図2: SSH トンネル経由で PRIVATE SERVER に接続する方法。
API では、トンネルを初期化して開始するか、トンネルの開始 と停止 を処理する with コンテキストを使用するかのいずれかが可能です:
上記の 図1 に対応するコードは以下のとおりです。リモートサーバーのアドレスは
pahaz.urfuclub.ru、パスワード認証、ローカルバインドポートはランダムに割り当てられます。
.. 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()
直接到達できないプライベートサーバーへのポートフォワーディングの例です。パスワードで保護された pkey 認証を想定し、リモートサーバーの SSH サービスはポート 443 で待ち受けており、そのポートはファイアウォールで開放されています(図2):
.. 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!')
Vagrant の MySQL ローカルポートに対するポートフォワーディングの例:
.. 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!')
または単に CLI を使用する場合:
.. code-block:: console
(bash)$ python -m sshtunnel -U vagrant -P vagrant -L :3306 -R 127.0.0.1:3306 -p 2222 localhost
2 つのトンネルを経由して SSH セッションを開く例。SSH トランスポートとトンネルは デーモン化され、クローズ時に接続の停止を待たない。
.. 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
options:
-h, --help このヘルプメッセージを表示して終了します
-U SSH_USERNAME, --username SSH_USERNAME
SSH サーバーのアカウントユーザー名
-p SSH_PORT, --server_port SSH_PORT
SSH サーバーの TCP ポート (デフォルト: 22)
-P SSH_PASSWORD, --password SSH_PASSWORD
SSH サーバーのアカウントパスワード
-R IP:PORT [IP:PORT ...], --remote_bind_address IP:PORT [IP:PORT ...]
リモートバインドアドレスのシーケンス: ip_1:port_1 ip_2:port_2 ... ip_n:port_n
ssh -Lxxxx:IP_ADDRESS:PORT と同等です
ポートを省略した場合は、デフォルトで 22 になります。
例: -R 10.10.10.10: 10.10.10.10:5900
-L [IP:PORT ...], --local_bind_address [IP:PORT ...]
ローカルバインドアドレスのシーケンス: ip_1:port_1 ip_2:port_2 ... ip_n:port_n
要素は有効な UNIX ソケットドメインでもかまいません:
/tmp/foo.sock /tmp/bar.sock ... /tmp/baz.sock
ssh -LPORT:xxxxxxxxx:xxxx と同等で、ローカル IP アドレスは省略可能です。
デフォルトではすべてのインターフェース (0.0.0.0) で待ち受け、ランダムなポートを選択します。
例: -L :40000
-k SSH_HOST_KEY, --ssh_host_key SSH_HOST_KEY
ゲートウェイのホストキー
-K KEY_FILE, --private_key_file KEY_FILE
RSA/DSS/ECDSA 秘密鍵ファイル
-S KEY_PASSWORD, --private_key_password KEY_PASSWORD
RSA/DSS/ECDSA 秘密鍵のパスワード
-t, --threaded 各トンネルへの同時接続を許可します
-v, --verbose 出力の詳細度を上げます (デフォルト: ERROR)
-V, --version バージョン番号を表示して終了します
-x IP:PORT, --proxy IP:PORT
宛先への SSH プロキシの IP とポート
-c SSH_CONFIG_FILE, --config SSH_CONFIG_FILE
SSH 設定ファイル (デフォルト: ~/.ssh/config)
-z, --compress SSH トランスポート上での圧縮をサーバーに要求します
-n, --noagent SSH エージェントからの鍵の検索を無効にします
-d [FOLDER ...], --host_pkey_directories [FOLDER ...]
SSH 公開鍵 (`id_*` 形式) が見つかる可能性のあるディレクトリのリスト