February 26, 2019

keep ssh running in background for tunneling

1. write the following script to a file, named "tunnel.sh" and make it executable (make sure user has public auth enabled on remote host):

while true; do ssh -t -n -R 127.0.0.1:2233:127.0.0.1:22 user@remote.host.com "while true; do ps -ef; sleep 1; done" ; sleep 1; done

2.  Run the the above script in a detached screen session:
    screen -S tunnel -d -m /path/to/tunnel.sh


 That's all. This creates a background screen session, which runs the tunnel.sh script, which loops an ssh command to keep it up and running.