3

I use this command to make an SSH tunnel to a remote server.

ssh -D localproxy:9999 -C [email protected]

I also made a shell script using expect command to auto process entire process with also providing password automatically.

Problem is, I have to manually run this script to make a connection when PC starts and also, when PC goes to sleep, this connection breaks and I run this script again.

Is there a way I can make it persistent regardless of the state of machine?

2 Answers 2

4

There are a lot of reasons that can make a connection fail, even using keepalives and things like that; so you should think about a mechanism to restart it automatically.

What I would do is:

  1. set up your SSH endpoint so that you can log in with a key in password-less mode: How can I set up password-less SSH login?

  2. Prepare a script that do the connection in an infinite loop --- restarting if failing; use "keepalive" nonetheless;

  3. launch it at boot: How do I get a program to start up at boot?

Notice that probably you will need to use su -c (or sudo) to run the ssh as your user --- commands in /etc/rc.local are run as root. (Thanks @muru).

3
  • 2
    Since this is SSH, one would probably want to use sudo to get it to run as the user when running from rc.local.
    – muru
    Apr 11, 2015 at 8:17
  • @muru you are right.
    – Rmano
    Apr 11, 2015 at 8:27
  • See also askubuntu.com/a/610379/16395
    – Rmano
    Apr 29, 2015 at 9:51
4

You say you want the connection to automatically restart if it is disconnected. There is a package to do that: autossh. See, e.g. here for how to use it. That link also has a start-up script for making it restart on boot, though that's relatively trivial; it's the restarting on connection failure which is hard.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .