Can I make SSH fail when a port forwarding fails?

If I do a remote port forward, a la -R 3690:localhost:3690 when a binding already exists on the port on the remote host, I get this warning:

Warning: remote port forwarding failed for listen port 3690

Is there a way to have ssh fail (i.e. exit with a nonzero return code), rather than just emit a warning?

2

2 Answers

Run

ssh -o ExitOnForwardFailure=yes ...

or put

ExitOnForwardFailure yes

into ~/.ssh/config. See man ssh_config for details.

4

I use bash script on the target host to make sure the forwarding was opened correctly. The SSH connection will run this and exit if there's a problem with the port forwarding, e.g.

client side script: ( this uses .ssh/config for port forwarding settings )

#!/bin/bash
while true; do echo -n starting at : " date ssh user@server bin/sshloop.sh echo "got back, sleeping 17 " sleep 17
done 

server side script ( bin/sshloop.sh )

#!/bin/bash
while true; do echo $(date)" : SSH Reverse 1090:80, 1232:22 From Server to Client" sleep 17 if ! netstat -an | grep -q ":::1090 " ; then echo "1090 forward missing, bailing out" exit fi
done

Maybe even run the client side script under screen with -dmS

3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like