如何将连接超时设置为sshtunnel

时间:2019-04-22 19:40:38

标签: python ssh-tunnel

我在python的sshtunnel模块中使用SSHTunnelForwarder。

给出下一个代码:

server = SSHTunnelForwarder(
    (sshServer, sshPort),
    ssh_username=sshUsername,
    ssh_password=sshPassword,
    remote_bind_address=(imapServer, imapPort),
    local_bind_address=('127.0.0.1', localPort)
)

print('STARTING')

server.start()

print(server.is_active)

有时会花费很长时间,也许要花几分钟,然后我会收到错误消息:

2019-04-22 22:25:54,365| ERROR   | Could not connect to gateway ip..... : 110
Traceback (most recent call last):
  File "PYTHON_TUNNEL.py", line 24, in <module>
    server.start()
  File "/home/mike/.local/lib/python2.7/site-packages/sshtunnel.py", line 1295, in start
    reason='Could not establish session to SSH gateway')
  File "/home/mike/.local/lib/python2.7/site-packages/sshtunnel.py", line 1101, in _raise
    raise exception(reason)
sshtunnel.BaseSSHTunnelForwarderError: Could not establish session to SSH gateway

Ubuntu 18.04,Python 2.7

是否可以为建立隧道设置超时时间?

1 个答案:

答案 0 :(得分:1)

SSH_TIMEOUT上设置了socket used to create the transport常量,并且在{{1}中设置了默认值None,然后再对其调用sshtunnel.py

不幸的是,我自己无法测试,但是您可以在启动隧道转发器之前尝试修补该常量:

connect

还有一个TUNNEL_TIMEOUT常量,但这似乎用于checks if the tunnel is already establishedopen_channel上的underlying Paramiko Transport instance调用。此外,import sshtunnel sshtunnel.SSH_TIMEOUT = 5.0 server = sshtunnel.SSHTunnelForwarder([...]) server.start() 默认设置为一秒,并且TUNNEL_TIMEOUTserver.start()上会经历一分钟的延迟,而在隧道创建过程中似乎没有作用。 BaseSSHTunnelForwarderError被提出,这个常数似乎不太可能与您的问题的解决方案相关。