how to have ssh fails if remote forwarding fails

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm using this command just to forward a port on a distant host:

ssh remotehost -Nnx -o ControlPath=none -o BatchMode=yes -R

I'm running this command in a while loop to have it always up even if
the connection timeout because connectivity problem. And sometimes, when
it is relaunched, it fails because of the following error:

Warning: remote port forwarding failed for listen port 2222

In this case, I'd like ssh to exit and my while loop will launch it
again until it succeed. Is it possible ?

Note that with the -Nnx options, if the TCP forwarding failed, the
command is pretty useless.

Thank you for your help
Best regards

Re: how to have ssh fails if remote forwarding fails

Quoted text here. Click to load it

Won't 'autossh' serve your needs? It's available as a system tool in
most Linux distributions, and can be found at =

Re: how to have ssh fails if remote forwarding fails


Nico Kadel-Garcia wrote :
Quoted text here. Click to load it

Thank you for the answer.
I have read the man page of autossh attentively but it doesn't seem to
fit my need. Basically, autossh is the while loop I have set in shell
script, and nothing more. The "loop of SSH forwarding" that autossh sets
up is just a way to monitor that the session didn't fail. With recent
version of SSH having ServerAliveInterval and ServerAliveCountMax
option, SSH does this monitoring itself, so this isn't necessary any
longer. I could use autossh any way to avoid the bash while loop, but
this doesn't really bring more features.

What I'd like is a reliable way to know, when SSH is launched, if the
remote forwarding (-R) succeeded or not, or better make ssh exit if it
fails instead of just outputting a warning. I could grep the ssh output,
do you if there is a better way ?

Thanks in advance

Site Timeline