我在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
是否可以设置隧道建立的超时?
有一个SSH_TIMEOUT
常数,在None
中默认为sshtunnel.py
,在socket used to create the transport上设置之前调用connect
。
不幸的是,我不能自己测试,但你可以尝试在启动隧道转发器之前修补该常量:
import sshtunnel
sshtunnel.SSH_TIMEOUT = 5.0
server = sshtunnel.SSHTunnelForwarder([...])
server.start()
还有一个TUNNEL_TIMEOUT
常数,但这似乎用于checks if the tunnel is already established和open_channel
上underlying Paramiko Transport instance的调用。在创建隧道期间,它似乎没有发挥作用。
另外,TUNNEL_TIMEOUT
默认设置为1秒,并且在server.start()
被提升之前,您在BaseSSHTunnelForwarderError
上经历了长时间的延迟,这个常数似乎不太可能与您的问题的解决方案相关。