BEQLEUNXP1NB103 - Cygwin - Files: Difference between revisions

From miki
Jump to navigation Jump to search
(new version using autossh)
 
(10 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== sshproxy ==

Download [{{#filelink: sshproxy}} this file].
{{#fileanchor: sshproxy}}
{{#fileanchor: sshproxy}}
<source lang="bash">
<source lang="bash">
#! /bin/sh
#! /bin/sh
#
#
# sshproxy Setup ssh port forwarding (imap) and SOCKS 5 proxy
# sshproxy Setup ssh port forwarding (imap) and SOCKS 5 proxy
# ssh connection is made persistent thanks to autossh
# ssh connection is made persistent thanks to autossh
#
#
Line 19: Line 22:
. /etc/rc.d/init.d/functions
. /etc/rc.d/init.d/functions


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


start()
[ -f $TARGET ] || exit 0
{

RETVAL=0

# See how we were called.
case "$1" in
start)
echo -n $"Starting $SUBSYS:"
echo -n $"Starting $SUBSYS:"
[ -f $PID_FILE ] && echo_failure && exit 1


if [ -f $LOCK_FILE ]; then
AUTOSSH_PIDFILE=$PID_FILE
RETVAL=1
AUTOSSH_POLL=300
else
/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
export AUTOSSH_PIDFILE=$PID_FILE
RETVAL=$?
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


echo -n -e $"\rStarting $SUBSYS:"
if [ $RETVAL -eq 0 ]; then
if [ $RETVAL -eq 0 ]; then
touch /var/lock/subsys/$SUBSYS
touch $LOCK_FILE
echo_success
echo_success
else
else
echo_failure
echo_failure
fi
fi
echo
;;
}
stop)

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

echo -n $"Stopping $SUBSYS:"
echo -n $"Stopping $SUBSYS:"

if [ ! -f $PID_FILE ]; then echo_failure; exit 1; fi
status
read PID < $PID_FILE
case "$RETVAL" in
kill -SIGKILL $PID>/dev/null
0)
rm $PID_FILE
( kill -TERM $pid || kill -KILL $pid ) >/dev/null
RETVAL=$?
RETVAL=$?
;;
1) RETVAL=0;;
2) RETVAL=0;;
*) ;;
esac

if [ $RETVAL -eq 0 ]; then
if [ $RETVAL -eq 0 ]; then
rm -f /var/lock/subsys/$SUBSYS
rm -f $LOCK_FILE
rm -f $PID_FILE
echo_success
echo_success
else
else
echo_failure
echo_failure
fi
fi
echo
;;
}
status)
# First try "pidof"
pid=`pidof -o $$ -o $PPID -o %PPID -x $TARGET`
if [ "$pid" != "" ] ; then
echo $"$SUBSYS (pid $pid) is running..."
exit 0
fi


status()
# Next try "/var/run/*.pid" files
{
if [ -f $PID_FILE ] ; then
# no lockfile --> (3) Not started.
read pid < $PID_FILE
# lockfile present, no pidfile --> (2) Started, but terminated
if [ "$pid" != "" ] ; then
# lockfile & pidfile present, no process --> (1) Started, but killed
echo $"$SUBSYS dead but pid file exists"
# lockfile, pidfile & process present --> (0) Started.
exit 1
# -- note this is opposite order compared to status function, but support multiple autossh running concurrently
fi

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
fi
}
# See if /var/lock/subsys/$SUBSYS exists

if [ -f /var/lock/subsys/$SUBSYS ]; then
reload()
echo $"$SUBSYS dead but subsys locked"
{
exit 2
read pid 2>/dev/null < $PID_FILE
fi
kill -HUP $pid
echo $"$SUBSYS is stopped"
exit 3
;;
refresh)
read PID < $PID_FILE
kill -s SIGHUP $PID
;;
restart)
$0 stop
echo -ne "\n"
$0 start
RETVAL=$?
RETVAL=$?
}
;;

*)
# See how we were called.
echo "Usage: $SUBSYS {start|stop|status|restart}"
case "$1" in
exit 1
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
esac



Latest revision as of 11:43, 26 April 2013

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