Last active
February 28, 2016 13:33
-
-
Save rahulsom/2949565 to your computer and use it in GitHub Desktop.
SSH Tunneling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# SSH Tunnel Manager | |
# | |
# SSH Flags: | |
# f - Force to background. | |
# N - Execute no commands. Don't open shell. | |
# q - Run quietly. | |
# T - Don't allocate pseudo TTY. | |
# R - Reverse Proxy. | |
# L - Local proxy. | |
# | |
# Usage: | |
# bash <(curl -skL http://bit.ly/ssh-tunnel) -server tunneluser@gateway.com | |
# | |
GATEWAY="" | |
BASEPORT=18000 | |
usage() { | |
cat << EOF | |
$0 | |
Establishes SSH Tunnels from Client and Server to a Gateway | |
Usage: $0 <-server|-client|-close> GATEWAY BASEPORT | |
-server Run server Mode | |
-client Run client Mode | |
-close Close open connections | |
GATEWAY Login on a server | |
BASEPORT Port to open tunnel on | |
BASEPORT must be unique for each server hooking up to the gateway. | |
PASSWORDS | |
========= | |
You will be asked to enter password to setup the SSH tunnel each time. To avoid typing in passwords, | |
you might want to use SSH keys. | |
EOF | |
} | |
server() { | |
echo BASEPORT is $BASEPORT | |
echo "Setting up tunnel for SSH: $((BASEPORT))" | |
nohup ssh -fNTR $((BASEPORT)):localhost:22 $GATEWAY | |
} | |
client() { | |
echo BASEPORT is $BASEPORT | |
echo "Setting up tunnel for SSH: $((BASEPORT))" | |
nohup ssh -fNqL $((BASEPORT)):localhost:$((BASEPORT )) $GATEWAY | |
} | |
close() { | |
case $OSTYPE in | |
darwin*) | |
FIELDNUM=3 | |
;; | |
*) | |
FIELDNUM=2 | |
;; | |
esac | |
echo $FIELDNUM | |
ps -ef | grep ssh | grep $((BASEPORT)) && \ | |
ps -ef | grep ssh | grep $((BASEPORT)) | tr -s " " | cut -d " " -f ${FIELDNUM} | xargs -n 1 kill -9 | |
} | |
if [ "$3" != "" ]; then | |
BASEPORT=$3 | |
fi | |
if [ "$2" != "" ]; then | |
GATEWAY=$2 | |
fi | |
if [ "$GATEWAY" = "" ]; then | |
usage | |
exit 1 | |
fi | |
case $1 in | |
-server) | |
echo Running SSH Tunnel in Server Mode | |
server | |
;; | |
-client) | |
echo Running SSH Tunnel in Client Mode | |
client | |
;; | |
-close) | |
echo Closing open SSH Tunnels | |
close | |
;; | |
*) | |
usage | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment