BEQLEUNXP1NB103 - Cygwin - Files: Difference between revisions

From miki
Jump to navigation Jump to search
(New page: {{#fileanchor: sshproxy}} <source lang="bash"> #! /bin/sh # # sshproxy Setup ssh port forwarding (imap) and SOCKS 5 proxy # # Author: Michaël Peeters # # chkconfi...)
 
(new version using autossh)
Line 4: Line 4:
#
#
# 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
#
#
# Author: Michaël Peeters
# Author: Michaël Peeters
#
# Prerequisite: ssh-agent must be running in background (autossh won't ask for password)
#
#
# chkconfig: 235 70 40
# chkconfig: 235 70 40
Line 11: Line 14:


SUBSYS=sshproxy
SUBSYS=sshproxy
TARGET=/usr/bin/ssh
TARGET=/usr/bin/autossh


# Source function library.
# Source function library.
Line 29: Line 32:
[ -f $PID_FILE ] && echo_failure && exit 1
[ -f $PID_FILE ] && echo_failure && exit 1


AUTOSSH_PIDFILE=$PID_FILE
oldPID=`pidofproc $TARGET`
AUTOSSH_POLL=300
$TARGET -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
/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=$?
RETVAL=$?
newPID=`pidofproc $TARGET`
uniqPID=`echo $oldPID $newPID|sed -e 's/ /\n/g'|sort|uniq -u`
echo $uniqPID>$PID_FILE


echo -n -e $"\rStarting $SUBSYS:"
echo -n -e $"\rStarting $SUBSYS:"
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$SUBSYS && echo_success || echo_failure
if [ $RETVAL -eq 0 ]; then
touch /var/lock/subsys/$SUBSYS
echo_success
else
echo_failure
fi
;;
;;
stop)
stop)
echo -n $"Stopping $SUBSYS:"
echo -n $"Stopping $SUBSYS:"
if [ ! -f $PID_FILE ]; then echo_failure; exit 1; fi
if [ ! -f $PID_FILE ]; then echo_failure; exit 1; fi
PID=`cat $PID_FILE`
read PID < $PID_FILE
kill -9 $PID>/dev/null
kill -SIGKILL $PID>/dev/null
rm $PID_FILE
RETVAL=$?
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
if [ $RETVAL -eq 0 ]; then
rm -f /var/lock/subsys/$SUBSYS
rm -f /var/lock/subsys/$SUBSYS
rm $PID_FILE
echo_success
echo_success
else
else
Line 76: Line 82:
echo $"$SUBSYS is stopped"
echo $"$SUBSYS is stopped"
exit 3
exit 3
;;
refresh)
read PID < $PID_FILE
kill -s SIGHUP $PID
;;
;;
restart)
restart)

Revision as of 21:00, 21 September 2009

{{#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 functions to make the below more readable
PID_FILE=/var/run/$SUBSYS.pid

[ -f $TARGET ] || exit 0

RETVAL=0

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

	AUTOSSH_PIDFILE=$PID_FILE
	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=$?

	echo -n -e $"\rStarting $SUBSYS:"
	if [ $RETVAL -eq 0 ]; then
		touch /var/lock/subsys/$SUBSYS
		echo_success
	else
		echo_failure
	fi
	;;
  stop)
	echo -n $"Stopping $SUBSYS:"
	if [ ! -f $PID_FILE ]; then echo_failure; exit 1; fi
	read PID < $PID_FILE
	kill -SIGKILL $PID>/dev/null
	rm $PID_FILE
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
		rm -f /var/lock/subsys/$SUBSYS
		echo_success
	else
		echo_failure
	fi
	;;
  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

	# Next try "/var/run/*.pid" files
	if [ -f $PID_FILE ] ; then
	        read pid < $PID_FILE
	        if [ "$pid" != "" ] ; then
	                echo $"$SUBSYS dead but pid file exists"
	                exit 1
	        fi
	fi
	# See if /var/lock/subsys/$SUBSYS exists
	if [ -f /var/lock/subsys/$SUBSYS ]; then
		echo $"$SUBSYS dead but subsys locked"
		exit 2
	fi
	echo $"$SUBSYS is stopped"
	exit 3
	;;
  refresh)
    read PID < $PID_FILE
	kill -s SIGHUP $PID
	;;
  restart)
  	$0 stop
	echo -ne "\n"
	$0 start
	RETVAL=$?
	;;
  *)
	echo "Usage: $SUBSYS {start|stop|status|restart}"
	exit 1
esac

exit $RETVAL