#!/bin/bash basename=`basename $0` sleepTime=300 defSshTunPort=22 function usage { cat < USAGE exit 1 } # arg parsing getoptResults=`getopt -o p:u:H:r:l:h --long user:,host:,remote-port:,local-port:,help -n $basename -- "$@"` if [ $? != 0 ] ; then usage ; fi # Note the quotes around `$TEMP': they are essential! eval set -- "$getoptResults" while true ; do case "$1" in -p|--ssh-port) optSshPort="$2" ; shift 2 ;; -u|--user) optUser="$2" ; shift 2 ;; -H|--host) optHost="$2" ; shift 2 ;; -r|--remote-port) optRemotePort="$2" ; shift 2 ;; -l|--local-port) optLocalPort="$2" ; shift 2 ;; -h|--help) optHelp=1 ; shift ;; --) shift ; break ;; *) echo "Internal error!" ; exit 1 ;; esac done echo "[$optHost]" # validate the args if [ "$optHelp" = 1 ]; then usage ; fi if [ -z "$optHost" ] then echo "Remote host required!" echo usage fi if [ "$optRemotePort" = "" ] then echo "Remote port required!" echo usage fi if [ "$optLocalPort" = "" ] then echo "Local port required!" echo usage fi optUser=${optUser:-$USER} optSshPort=${optSshPort:-$defSshTunPort} command="ssh -p $optSshPort -N -R $optRemotePort:localhost:$optLocalPort $optUser@$optHost" # This will keep trying to restore the tunnel any time it's down while true do echo -n `date` echo " Starting tunnel with this command:" echo " $command" echo "Press ctrl-c twice to REALLY stop" $command echo "Stopped at $(date) Restarting in $sleepTime secs" sleep $sleepTime echo "Restarting.." done