为什么在远程转发失败时不会退出?

时间:2017-02-21 02:02:29

标签: bash ssh software-design

我有一个使用ssh进行远程转发的bash脚本,这样我就可以登录运行脚本的机器了。我使用脚本是因为网络需要定期进行某种身份验证。如果ssh失败,我会重试。脚本基本上是这样的:

while [ 1 ]; do
    authentication
    ssh -N -v -R 9999:localhost:22 user@$remote_ip
done

问题是ssh在远程转发失败时退出,如下所示:

debug1: Remote: Forwarding listen address "localhost" overridden by server GatewayPorts
debug1: remote forward failure for: listen 9999, connect localhost:22
Warning: remote port forwarding failed for listen port 9999
debug1: All remote forwarding requests processed

失败的原因是:

debug1: server_input_global_request: tcpip-forward listen localhost port 9999
debug1: Local forwarding listening on 0.0.0.0 port 9999.
bind: Address already in use
channel_setup_fwd_listener: cannot listen to port: 9999

上一个会话尚未结束,端口正在使用中。

有没有办法检查ssh是否成功? 作为一名程序员,我真的无法理解为什么它以这种方式设计。这个设计的基本原理是什么?

2 个答案:

答案 0 :(得分:0)

您的ssh会话在服务器端有超时,因此当您尝试重新连接时,之前的连接仍在侦听端口9000.

使用openvpn,而不是这个hacky解决方案。

答案 1 :(得分:0)

如果无法设置端口转发,则可以传入配置参数df=pd.read_sql_query(“””select * from table “””, conn) 来告诉ssh客户端终止连接。

  

ExitOnForwardFailure   指定如果ssh(1)无法设置所有请求的动态,隧道,本地和远程端口转发,则是否应终止连接。参数必须是“是”或“否”。默认值为“否”。

类似的东西:

ExitOnForwardFailure