I've set up port forwarding over ssh so I can securely connect to a web console. Since the bash command is pretty long, I want to create a small script that will do the following;

  • Check if there is an open ssh connection to the server in question.
  • If it's not open, open it.
  • Open browser to the console I want to access.

This is what I have so far


ssh -N -l my_username -i ~/.ssh/id_rsa_ssh -L <port>:<ip>:<port> <forwarding_ip> > /dev/null 2>&1 < /dev/null &

open -a Firefox http://localhost:<port>/url/to/something/secure

The ssh connection opens, but the browser can't establish a connection. Is there any way to programmatically open an ssh connection, background it, and ensure it stays open?


1 Answer 1


You wanted autossh.

autossh - Automatically restart SSH sessions and tunnels

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.