首页 > 解决方案 > 将 ssh 隧道命令翻译成 SSHTunnelForwarder 代码?

问题描述

我正在尝试将此 ssh 隧道命令转换为使用 python SSHTunnelForwarder。这有效:

ssh -i mypk -N -L 5901:localhost:5432 user@100.0.0.100

我试过这个:

with SSHTunnelForwarder(
        ('localhost', 5901),
        ssh_username='user',
        ssh_private_key=path_to_mypk,
        remote_bind_address=('100.0.0.100', 5432)

但是出现错误(在它暂停几秒钟后):“无法建立与 SSH 网关的会话”。我应该做什么?

标签: pythonsshssh-tunnel

解决方案


我缺少的部分是从服务器的角度使用“localhost”。以下作品:

with SSHTunnelForwarder(
        ('100.0.0.100', 22),
        ssh_username='user',
        ssh_private_key=path_to_mypk,
        remote_bind_address=('localhost', 5432), # localhost from server's perspective
        local_bind_address=('localhost', 5901)  # any available port
)

推荐阅读