autossh init script

autossh init script

autossh init script 150 150 Roderick Derks

Enjoy this script that will help you to initiate SSH connections automatically.

 

#!/bin/sh
#
# autossh . Startup script for autossh

# chkconfig: 2345 20 80
# description: Maintain a persistent SSH tunneling
# processname: autossh
# pidfile: /var/run/autossh.pid

# @since 2012-02-22 15:31:47
# @author Roderick Derks

# Source function library
. /etc/init.d/functions

prog="autossh"
autossh="/usr/bin/autossh"
RETVAL=0
AUTOSSH_PIDFILE=/var/run/autossh.pid

# Tunnel configuration
LOCAL_PORT_LISTEN=12121
REMOTE_DESTINATION_PORT=5669
USER=nagios
REMOTE_DESTINATION_IP=192.168.0.100

start() {
 echo -n $"Starting $prog: "
 if [ ! -e $AUTOSSH_PIDFILE ]; then
    AUTOSSH_PIDFILE=$AUTOSSH_PIDFILE;export AUTOSSH_PIDFILE

    # Got a problem using the daemon function, the ssh tunnel will not start as it should. So commented out that line.
    #daemon autossh -M 0 -q -f -N -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -L $LOCAL_PORT_LISTEN:localhost:$REMOTE_DESTINATION_PORT $USER@$REMOTE_DESTINATION_IP

   
autossh -M 0 -q -f -N -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -L $LOCAL_PORT_LISTEN:localhost:$REMOTE_DESTINATION_PORT $USER@$REMOTE_DESTINATION_IP
    RETVAL=$?
 else
    RETVAL=1
    echo_failure
    echo pid file still exists $AUTOSSH_PIDFILE
 fi
 echo
 [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
 return $RETVAL
}

stop() {
 echo -n $"Stopping $prog: "
 killproc $autossh
 RETVAL=$?
 echo
 [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog && rm -f $AUTOSSH_PIDFILE
 return $RETVAL
}

case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
restart)
        stop
        start
        ;;
status)
        status $autossh
        RETVAL=$?
        ;;
*)
        echo $"Usage: $0 {start|stop|restart|status}"
        RETVAL=1

Roderick Derks

Liefhebber van fietsen, van het oplossen van IT puzzels, en van het delen van informatie om anderen te helpen.

All stories by:Roderick Derks

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

    Your Name (required)

    Your Email (required)

    Subject

    Your Message

      Your Name (required)

      Your Email (required)

      Subject

      Your Message