SSH port forwarding without session

port-forwardingssh

I am trying to forward my port 8085 which is a live camera stream port for a mini http server, to a remote server.

My command which I am using is as so:

ssh -R $rport:dc-bb7925e7:$camport -p 25 server@192.168.178.20

As this command is right now, it does forward the port and I can see the live stream on the remote server by going to localhost:8085, BUT the problem is that on the client, there is a TTY session open which prevents all further scripts from running.

So I tried using ssh in the background with:

ssh -Nf -R $rport:dc-bb7925e7:$camport -p 25 server@192.168.178.20

This does not work as it seems as if the connection is closed. The port forwarding is being used in a script where the script assesses an if/then/else condition and then executes to forward. Forwarding this port is not suposed to halt all other scripts. It should simply forward the port and then move on, while keeping it open.

What am I doing wrong or are there any other flags that can be used?

Best Answer

Rather than backgrounding an active TTY, you can just let SSH take care of things by itself.

My suggestion is this:

ssh -fNT -R $rport:dc-bb7925e7:$camport -p 25 server@192.168.178.20

The first three options are:

  -f    Requests ssh to go to background just before command execution.
  -N    Do not execute a remote command.
  -T    Disable pseudo-tty allocation.

The -f option is the one that actually backgrounds things, but -N and -T will save resources which you don't need to allocate for an SSH session whose sole purpose is to carry your tunnel.

Also note that some of these options can be added to a custom profile in your ~/.ssh/config file, in case you feel it would be preferable to put more of your static configuration into static configuration files rather than scripts. The RemoteForward config file option is equivalent to the -R command line option.

See also:

Related Question