All pastes #983612 Raw Edit

Miscellany

public shellscript v1 · immutable
·published 2008-04-13 14:27 UTC
rendered paste body
#! /bin/bash## Starts redirecting tcp connections from the internet to the local network## Exits on failureset -e# Default paths and optionsDAEMON="/usr/bin/redir"DAEMON_OPTS="--lport 8888 --cport 8888 --caddr 192.168.0.170"# Check if daemon is executable, otherwise exit scripttest -x $DAEMON || exit 0# Load standard text functions. /lib/lsb/init-functions# Fill in a default pid of 1 if pidfile doesn't exist yetif [ ! -e /var/run/redir.pid ]; then  echo "1" > /var/run/redir.pidfi# Custom Functions:# simply tells the redir daemon to start, only tries starting the daemon once# outputs "[fail]" on failure, or "[OK]" on success, no other output or action is takenstart_redir_daemon() {	start-stop-daemon --start --quiet --pidfile /var/run/redir.pid --make-pidfile --background --exec $DAEMON -- $DAEMON_OPTS || log_end_msg 1	# "[fail]"	log_end_msg 0	# "[OK]"}# tries to stop the daemon up to 5 times (with 1 second pause inbetween) before giving up# outputs "[fail]" and a warning and then exits script on failure, otherwise outputs "[OK]" on successstop_redir_daemon() {	COUNTER=0	while ( ps -p $(cat /var/run/redir.pid) | grep -q redir ); do		let COUNTER=COUNTER+1 		if [ "$COUNTER" -ge "5" ]; then			log_end_msg 1		# end the previous message with "[fail]"			log_warning_msg "Unable to stop Redir Daemon after 5 retries."			exit 1			# exit script		fi		start-stop-daemon --stop --quiet --oknodo --pidfile /var/run/redir.pid		sleep 1				# wait 1 second before checking if process was successfully killed	done	log_end_msg 0				# end the previous message with "[OK]" on success}# Main Logic:case "$1" in	start)		if ( ps -p $(cat /var/run/redir.pid) | grep -q redir ); then			log_warning_msg "Redir Daemon is already running."		else			log_begin_msg "Starting Redir Daemon..."			start_redir_daemon		fi		;;	stop)		if ( ps -p $(cat /var/run/redir.pid) | grep -q redir ); then			log_begin_msg "Stopping Redir Daemon..."			stop_redir_daemon		else			log_warning_msg "Redir Daemon is already stopped."		fi		;;	restart)		log_begin_msg "Stopping Redir Daemon..."		stop_redir_daemon		log_begin_msg "Starting Redir Daemon..."		start_redir_daemon		;;	*)		log_success_msg "Usage: /etc/init.d/redir {start|stop|restart}"		exit 1esacexit 0