简体   繁体   English

如何将连接超时设置为sshtunnel

[英]How to set connection timeout to sshtunnel

I use SSHTunnelForwarder from sshtunnel module on python. 我在python的sshtunnel模块中使用SSHTunnelForwarder。

Given the next code: 给出下面的代码:

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)

Sometimes it take really long time, maybe few minutes, and then I get error: 有时需要很长时间,可能要花几分钟,然后我得到了错误:

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 Ubuntu 18.04,Python 2.7

Is it possible to set timeout for tunnel establishment ? 是否可以设置隧道建立超时?

There's an SSH_TIMEOUT constant with a default of None in sshtunnel.py that is set on the socket used to create the transport before calling connect on it. 这里有一个SSH_TIMEOUT用的默认常量Nonesshtunnel.py是在上设置用于创建传输插座之前调用connect就可以了。

Unfortunately, I can't test this myself, but you could try patching that constant before starting the tunnel forwarder: 不幸的是,我自己无法测试,但是您可以在启动隧道转发器之前尝试修补该常量:

import sshtunnel

sshtunnel.SSH_TIMEOUT = 5.0

server = sshtunnel.SSHTunnelForwarder([...])    
server.start()

There's also a TUNNEL_TIMEOUT constant, but that seems to be used for checks if the tunnel is already established and for the call to open_channel on the underlying Paramiko Transport instance . 还有一个TUNNEL_TIMEOUT常量,但这似乎用于检查隧道是否已建立以及在基础 open_channel Transport实例上对open_channel的调用。 It does not seem to play a role during the creation of the tunnel. 它在隧道创建期间似乎没有作用。
Plus, TUNNEL_TIMEOUT is set to one second by default, and with you experiencing minute-long delays on server.start() before the BaseSSHTunnelForwarderError is raised, it seems unlikely that this constant is relevant for a solution to your issue. 另外,默认情况下, TUNNEL_TIMEOUT设置为一秒,并且在BaseSSHTunnelForwarderError之前, server.start()会经历一分钟的延迟,因此该常量似乎不太可能与您的问题相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM