|测试| |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 发挥作用的一个典型场景。用户可能需要连接远程服务器的某个端口(例如 8080),而该服务器只有 SSH 端口(通常为 22 端口)可达。::
----------------------------------------------------------------------
|
-------------+ | +----------+
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
打开一个跨越两条隧道的 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_*`)的目录列表