Skip to content

Instantly share code, notes, and snippets.

@courtney-rosenthal
Created March 29, 2016 15:14
Show Gist options
  • Save courtney-rosenthal/f1008929b8fc603767bc to your computer and use it in GitHub Desktop.
Save courtney-rosenthal/f1008929b8fc603767bc to your computer and use it in GitHub Desktop.
Create a tunnel for email service, e.g. to get POP/IMAP through outbound port filters
#!/bin/sh
#
# email-tunnel - create a tunnel for email service, e.g. to get POP/IMAP through outbound port filters
#
# Prerequisites: daemon, ssh
#
# Uses the LocalForward feature of ssh.
#
# You'll need an "email-tunnel" entry in your .ssh/config file. Something like:
#
# Host email-tunnel
# HostName shell.soaustin.net
# User chip
# LocalForward 4143 mail.soaustin.net:143
#
# Chip Rosenthal
# chip@unicom.com
#
set -e
USAGE="usage: $0 {start|stop|restart|status} (default = \"start\")"
NAME=`basename $0`
PIDFILE=$HOME/var/run/$NAME.pid
mkdir -p `dirname $PIDFILE`
LOGFILE=$HOME/var/log/$NAME.log
mkdir -p `dirname $LOGFILE`
perform_start()
{
if perform_check ; then
echo "ERROR - service \"$NAME\" is already running" >&2
return 1
fi
echo "Starting service \"$NAME\" ..." >&2
daemon \
--name=$NAME \
--pidfile=$PIDFILE \
--errlog=$LOGFILE \
--output=$LOGFILE \
--respawn \
-- \
ssh -N -v email-tunnel
}
perform_stop()
{
if perform_check ; then
: nop
else
echo "ERROR - service \"$NAME\" is not running" >&2
return 1
fi
echo "Stopping service \"$NAME\" ..." >&2
daemon \
--name=$NAME \
--pidfile=$PIDFILE \
--stop
}
perform_check()
{
daemon \
--name=$NAME \
--pidfile=$PIDFILE \
--running
}
perform_status()
{
daemon \
--name=$NAME \
--pidfile=$PIDFILE \
--verbose=99 --running
}
case $# in
0) op="start" ;;
1) op="$1" ;;
*) echo "$USAGE" >&2 ; exit 1
esac
case "$op" in
start)
perform_start
;;
stop)
perform_stop
;;
status)
perform_status
exit 0 # to skip final message
;;
restart)
if perform_check ; then
perform_stop
sleep 3
else
echo "Service \"$NAME\" currently is not running." >&2
fi
perform_start
;;
*)
echo "$USAGE" >&2
exit 1
;;
esac
if perform_check ; then
echo "Service \"$NAME\" is running." >&2
else
echo "Service \"$NAME\" is stopped" >&2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment