如果端口转发失败,如何使ssh失败

我有一个bsh脚本运行ssh来创build一个端口转发,使用像这样的命令:

ssh -N -i keyfile -L 1000:localhost:22 *remote_ip* 

监听端口有时可能很忙,所以这个命令给出了错误:

 channel_setup_fwd_listener: cannot listen to port: 1000 Could not request local forwarding. 

但是,ssh连接保持不变,而ssh命令将被阻止。 当发生这种情况时,如何让ssh实际上失败,所以我的脚本可以处理它?

不幸的是,我还需要在Solaris(Intel)上支持这一点,并且那里的ssh命令不支持ExitOnForwardFailure选项 – 在这种情况下的任何想法?

如果您检查了ssh手册页,您会发现有一个名为ExitOnForwardFailure的configuration选项,您可以通过添加以下命令来在命令行中指定它:

-o "ExitOnForwardFailure yes"

在ssh_config和sshd_config手册页中介绍了所有的sshconfiguration选项。 如果您发现该选项不受支持,则可能需要升级到较新版本的ssh。

祝你好运。

你也可以在你的'~/.ssh/config'文件中添加ExitOnForwardFailure yes

创build一个,如果你没有一个。

确保正确的用户拥有该文件。

重启你的SSH服务sudo service ssh restart在unbuntu上sudo service ssh restart