BEQLEUNXP1NB103 - Cygwin - Files

From miki
Jump to navigation Jump to search

sshproxy

Download [{{#filelink: sshproxy}} this file]. {{#fileanchor: sshproxy}}

#! /bin/sh
#
# sshproxy            Setup ssh port forwarding (imap) and SOCKS 5 proxy
#					  ssh connection is made persistent thanks to autossh
#
# Author:             Michaël Peeters
#
# Prerequisite:		  ssh-agent must be running in background (autossh won't ask for password)
#
# chkconfig:          235 70 40
# description:        sshproxy startup script.

SUBSYS=sshproxy
TARGET=/usr/bin/autossh

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

# Some macros to make the below more readable
PID_FILE=/var/run/$SUBSYS.pid
LOCK_FILE=/var/lock/subsys/$SUBSYS

start()
{
	echo -n $"Starting $SUBSYS:"

	if [ -f $LOCK_FILE ]; then
		RETVAL=1
	else
		export AUTOSSH_PIDFILE=$PID_FILE
		export AUTOSSH_POLL=300
		/usr/bin/autossh -M 0 -f -N -n -q -L 9025:smtp.priorweb.be:25 -L 9143:mail.priorweb.be:143 -L 9026:mail.altranweb.be:25 -L 9144:mail.altranweb.be:143 -D 1080 noekeon
		RETVAL=$?
	fi

	if [ $RETVAL -eq 0 ]; then
		touch $LOCK_FILE
		echo_success
	else
		echo_failure
	fi
	echo
}

stop()
{
	# don't delete lock file while pid file not deleted
	# don't delete pidfile while process is running

	echo -n $"Stopping $SUBSYS:"

	status
	case "$RETVAL" in
	0)
		( kill -TERM $pid || kill -KILL $pid ) >/dev/null
		RETVAL=$?
		;;
	1) RETVAL=0;;
	2) RETVAL=0;;
	*) ;;
	esac

	if [ $RETVAL -eq 0 ]; then
		rm -f $LOCK_FILE
		rm -f $PID_FILE
		echo_success
	else
		echo_failure
	fi
	echo
}

status()
{
	# no lockfile                            --> (3) Not started.
	# lockfile present, no pidfile           --> (2) Started, but terminated
	# lockfile & pidfile present, no process --> (1) Started, but killed
	# lockfile, pidfile & process present    --> (0) Started.
	# -- note this is opposite order compared to status function, but support multiple autossh running concurrently

	unset pid
	read pid 2>/dev/null < $PID_FILE

	if [ ! -f /var/lock/subsys/$SUBSYS ]; then
		# /var/lock/subsys/$SUBSYS doesn't exist
		RETVAL=3
	elif [ "$pid" = "" ] ; then
		# lock file exists, but file "/var/run/*.pid" doesn't exist
		RETVAL=2
	elif ( ! ps -p $pid | grep -q $pid ) ; then
		# Both files exist but process is not running anymore...
		RETVAL=1
	else
		# Everything is fine
		RETVAL=0
	fi
}

reload()
{
	read pid 2>/dev/null < $PID_FILE
	kill -HUP $pid
	RETVAL=$?
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status
		case "$RETVAL" in
			0)
				echo -n $"$SUBSYS (pid $pid) is running"
				if ( ps -af | grep -q " $pid .*/ssh" ) ; then echo $" and connection is alive..."; else echo $" but connection is dead..."; fi
				;;
			1)
				echo $"$SUBSYS dead but pid file exists"
				;;
			2)
				echo $"$SUBSYS dead but subsys locked..."
				;;
			3)
				echo $"$SUBSYS is stopped"
				;;
		esac
		;;
	reload)
		reload
		;;
	restart)
		stop
		start
		;;
	*)
		echo "Usage: $0 {start|stop|status|reload|restart}"
		RETVAL=1
esac

exit $RETVAL