git clone https://gist.github.com/6322759.git autosshd
cd autosshd
sudo ./install
sudo vim /etc/autosshd/ssh_config
sudo ssh -F /etc/autosshd/ssh_config tunnel
sudo service autosshd startAnd then install /etc/autosshd/id_rsa.pub at target.
| #!/bin/bash | |
| ### BEGIN INIT INFO | |
| # Provides: autosshd | |
| # Required-Start: $network $remote_fs $syslog | |
| # Required-Stop: $network $remote_fs $syslog | |
| # Default-Start: 2 3 4 5 | |
| # Default-Stop: 0 1 6 | |
| # Short-Description: Start autossh | |
| ### END INIT INFO | |
| PATH=/sbin:/bin:/usr/sbin:/usr/bin | |
| . /lib/lsb/init-functions | |
| DAEMON=/usr/bin/autossh | |
| PIDFILE=/var/run/autosshd.pid | |
| AUTOSSH_OPTS="-M 0 -f -nNT -F /etc/autosshd/ssh_config tunnel" | |
| LOCKFILE=/var/lock/autosshd | |
| export AUTOSSH_PIDFILE="$PIDFILE" | |
| test -x $DAEMON || exit 5 | |
| lock_tunnel() { | |
| if [ -x /usr/bin/lockfile-create ]; then | |
| lockfile-create $LOCKFILE | |
| lockfile-touch $LOCKFILE & | |
| LOCKTOUCHPID="$!" | |
| fi | |
| } | |
| unlock_tunnel() { | |
| if [ -x /usr/bin/lockfile-create ] ; then | |
| kill $LOCKTOUCHPID | |
| lockfile-remove $LOCKFILE | |
| fi | |
| } | |
| case $1 in | |
| start) | |
| log_daemon_msg "Starting autossh" "autosshd" | |
| lock_tunnel | |
| start-stop-daemon --start --pidfile $PIDFILE --startas $DAEMON -- $AUTOSSH_OPTS | |
| status=$? | |
| unlock_tunnel | |
| log_end_msg $status | |
| ;; | |
| stop) | |
| log_daemon_msg "Stopping autossh" "autosshd" | |
| start-stop-daemon --stop --retry=TERM/30/KILL/5 --pidfile $PIDFILE | |
| log_end_msg $? | |
| rm -f $PIDFILE | |
| ;; | |
| restart|force-reload) | |
| $0 stop && sleep 2 && $0 start | |
| ;; | |
| try-restart) | |
| if $0 status >/dev/null; then | |
| $0 restart | |
| else | |
| exit 0 | |
| fi | |
| ;; | |
| reload) | |
| exit 3 | |
| ;; | |
| status) | |
| status_of_proc $DAEMON "autosshd" | |
| ;; | |
| *) | |
| echo "Usage: $0 {start|stop|restart|try-restart|force-reload|status}" | |
| exit 2 | |
| ;; | |
| esac |
| #!/bin/bash | |
| set -eu | |
| sudo apt-get install autossh | |
| config_dir='/etc/autosshd' | |
| key_file="$config_dir/id_rsa" | |
| ssh_config_file="$config_dir/ssh_config" | |
| if ! [ -e "$config_dir" ] ; then | |
| echo 'Creating config dir...' | |
| mkdir -p "$config_dir/" | |
| fi | |
| if ! [ -e "$key_file" ] ; then | |
| echo 'Generating keypair...' | |
| ssh-keygen -b 4096 -f "$key_file" -N '' | |
| fi | |
| if ! [ -e "$ssh_config_file" ] ; then | |
| echo 'Creating template ssh_config...' | |
| cat << EOF >> $ssh_config_file | |
| Host tunnel | |
| HostName server.example.com | |
| Port 1234 | |
| User bob | |
| ServerAliveInterval 30 | |
| ServerAliveCountMax 3 | |
| RemoteForward 127.0.0.1:10001 127.0.0.1:22 | |
| ExitOnForwardFailure yes | |
| IdentityFile $key_file | |
| UserKnownHostsFile $config_dir/known_hosts | |
| EOF | |
| fi | |
| echo 'Installing init.d script...' | |
| cp ./autosshd '/etc/init.d/' | |
| echo 'Configuring run levels...' | |
| update-rc.d autosshd defaults | |
| echo 'done.' |
statusisn't working for me, @haku. I'll let you know if I find a solution.