将ssh tunnel命令转换为SSHTunnelForwarder代码吗?

时间:2019-01-27 00:02:43

标签: python ssh ssh-tunnel

我正在尝试将此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网关的会话”。我该怎么办?

1 个答案:

答案 0 :(得分:0)

从服务器的角度来看,我缺少的部分是使用“ 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
)