SSH port forwarding with automatic reconnect, script executing at startup

4

1

In short, every time my computer boots, I would like to start a ssh port-forwarding connection that will reconnect with the server if the connection drops. Computer is Debian Wheezy x64.

An initial and successful connection can be made as follows:

screen -dmS autossh autossh -M 29000 -N -v -p 22 -l user -i /path/to/.ssh/id_rsa -L port:my-computer:port my-server.com

How can this be made to run at startup? (preferably by user, not root). Here is currently how I have been starting programs at boot:

rc.local calls my startup script as follows:

su -c "/home/username/scripts/startup" username

Currently, /home/username/scripts/startup contains the following:

screen -dmS program1 python my-python-program
screen -dmS program2 python my-python-program2
screen -dmS autossh autossh -M 29000 -N -v -p 22 -l user -i /path/to/.ssh/id_rsa -L port:my-computer:port my-server.com

Once booted, all three programs appear to be running (by list screen -ls). There is no problem with program1 or program 2. However, autossh is not actually forwarding the port.

The difference between a successful connection run by me, and an unsucessful connection run at startup is that a successful connection has the line:

debug1: channel 4: free: direct-tcpip: listening port 8082 for 192.168.1.104 port 80, connect from ::1 port 59681, 
nchannels 5

While the unsuccessful connection keeps trying like so:

debug1: Connection to port 8082 forwarding to 192.168.1.104 port 80 requested.
debug1: channel 4: new [direct-tcpip]
debug1: Connection to port 8082 forwarding to 192.168.1.104 port 80 requested.
debug1: channel 5: new [direct-tcpip]
debug1: Connection to port 8082 forwarding to 192.168.1.104 port 80 requested.
debug1: channel 6: new [direct-tcpip]
debug1: Connection to port 8082 forwarding to 192.168.1.104 port 80 requested.
debug1: channel 7: new [direct-tcpip]
debug1: Connection to port 8082 forwarding to 192.168.1.104 port 80 requested.
debug1: channel 8: new [direct-tcpip]

Any idea what is wrong? Why would a program work fine when I run it manually, but not inside my startup script?

Oddworld

Posted 2012-12-27T20:01:05.003

Reputation: 99

Sorry - dont have the time for a full answer, but : http://www.harding.motd.ca/autossh/ should help you along the way.

– FreudianSlip – 2012-12-27T20:06:44.013

Answers

3

Check out the program autossh for automatic reconnection. screen will not mess up because you always specify external commands and flags for those commands after screen specific flags.

ggustafsson

Posted 2012-12-27T20:01:05.003

Reputation: 1 698

Autossh works just fine, thank you. I am having a hard time getting it to start at boot. I like to use my startup script for everything (and it has worked fine so far starting everything else). However, it does not work for autossh. I have edited my question above, please take a look at it. – Oddworld – 2012-12-28T14:28:19.507

1Thank you for pointing me to autossh, that works great! Now, my only issue is getting it to run at startup. For some odd reason, it seems to work well when I run it by hand, but not when it is in my startup script. Odd – Oddworld – 2012-12-28T15:11:37.617