On Tue, Aug 21, 2012 at 07:59:30PM -0600, Luis F Urrea wrote:
> I have a server hosting SSH tunnels and Openbsd 4.5 clients connecting to
> it.
> 
> Things work just fine but I am in the need of automating the connection
> from the client to the server. So that if the client is accidentally
> rebooted, then the connection initiates unattended.
> 
> So it should be as straight forward as to include the ssh connection in an
> init script. However I have miserably failed to do so by including it to
> /etc/rc.local, which is the file I usually do this sort of things in.
> 
> Right now I am using autossh to also restart the connection if necessary
> and the script that I put on /etc/rc.local follows:
> 
> #!/bin/sh
> #
> # Example script to start up tunnel with autossh.
> #
> # This script will tunnel 2200 from the remote host
> # to 22 on the local host. On remote host do:
> #     ssh -p 2200 localhost
> #
> # $Id: autossh.host,v 1.6 2004/01/24 05:53:09 harding Exp $
> #
> 
> ID=root
> HOST=example.com
> 
> #AUTOSSH_POLL=600
> #AUTOSSH_PORT=20000
> #AUTOSSH_GATETIME=30
> #AUTOSSH_LOGFILE=$HOST.log
> #AUTOSSH_DEBUG=yes
> #AUTOSSH_PATH=/usr/local/bin/ssh
> export AUTOSSH_POLL AUTOSSH_LOGFILE AUTOSSH_DEBUG AUTOSSH_PATH
> AUTOSSH_GATETIME AUTOSSH_PORT
> 
> autossh -2 -f -M 20000 ${ID}@${HOST}
> 
> The script detaches just fine when run manually so I just include it on
> /etc/rc.local as
> 
> echo -n 'starting local daemons:'
> 
> if [ -x /usr/local/sbin/autossh.sh ]; then
>    echo -n 'ssh tunnel'
>    /usr/local/sbin/autossh.sh
> fi
> 
> echo '.'
> 
> 
> I have also tried calling it from /etc/hostname.tun0 in case there may be
> issues with /etc/rc.local not being called at the right time when network
> connections are ready, so I would use:
> 
> inet 10.254.254.2 255.255.255.252 10.254.254.1
> !/usr/local/sbin/autossh.sh
> 
> Your input is highly appreciated.
> 
> PS. Clients are remote to me so i dont see much from console and I cant
> seem to find a way to have logs from boot.
> 

I tried testing your script but hit a more general problem early on.
I'm running 5.1 and haven't been able to get autossh to work
successfully when called from the command line (or your script) using
the -f flag.  According to the logs (authlog) on the remote machine,
the client successfully connects, then disconnects, apparently of its
own accord:

Aug 21 20:06:03 sshd[4635]: Accepted publickey for paul from 192.168.44.8 port 
59113 ssh2
Aug 21 20:06:03 sshd[1671]: Received disconnect from 192.168.44.8: disconnected 
by user

And the log file on local host:

Aug 21 20:06:02 x200 autossh[4347]: starting ssh (count 1)
Aug 21 20:06:04 x200 autossh[4347]: ssh child pid is 26907
Aug 21 20:06:04 x200 autossh[4347]: ssh exited prematurely with status 1; 
autossh exiting

Calling autossh from your script had the same results (I didn't try it
using rc.local).  The return status of 1 on the very first attempt
apparently causes autossh to give up (as documented in the man page),
though I can't see a reason why ssh would disconnect.  

You might check what is in /var/log/authlog on your server. (BTW, the
AUTOSSH_DEBUG flag didn't produce any output.)

What's the -2 arg for in your script? I don't see it in the
manpage.

But it *does* work without the -f flag.

Reply via email to