#!/bin/bash
# tunnel script created by jtsop
# easily manage your encrypted(ssh) tunnels
# visit http://tsopokis.gr for more info
GW=$1

if [ "$GW" == "-s" ] || [ "$GW" == "--show" ]; then
	ps -f -C ssh | grep "ssh -L"
	exit;
fi


if [ $# -eq 0 ] ||  [ $# -eq 1 ] ||  [ $# -eq 2 ]; then
	GW="--help"
fi


if [ "$GW" == "--help" ] || [ "$GW" == "-h" ] || [ "$GW" == "?" ]; then
	echo "tunnel: create an ssh tunnel through a gateway."
	echo "	ssh tunnel script by jtsop"
        echo
        echo "tunnel usage:"
	echo "	tunnel [user@]gateway target port [source-port [ssh-port] ]"
	echo "	tunnel -s || --show"
	echo
	echo "for more details see the presentation at http://tsopokis.gr"
	exit;
fi

SRC_PORT=$4

if [ "$#" -eq 3 ]; then
        SRC_PORT=$3
fi

SSH_PORT=""
if [ "$#" -eq 5 ]; then
        SSH_PORT="-p "$5
fi


ssh $SSH_PORT -L $SRC_PORT:$2:$3 $GW -Nfg

