Saturday, September 27, 2014

SSH Port Forwarding Magic: Dynamic Port Forwarding Managment Script

For those whom are "in the know", you can skip reading this section. If not, then I am about to reveal an awesome secret to you. SSH stands for "Secure Shell" and is something that Linux users use everyday. It's pretty freaking badass and it's got lots of tricks up it's sleeve that many are not aware of. Okay, let's backup:

SSH (a secure shell) is a protocol to remotely, and securely access and control a *nix system. For instance, when I am maintaining my web server located in Switzerland, I don't travel to Switzerland to do so, I just access the server via SSH. But, what if I actually am in Switzerland and want to listen to some music on Pandora?

I'm going to get a "restricted" message, and no music, because Pandora (for whatever reason) only allows users with a United States IP address use their services. So, what to do about it? Well, one of my favourite things about SSH is that you can turn it into a quick, secure, and effective proxy server!

ssh -C -D 8080 user@host 

If you have a computer back home in the United States, then you can simply set your browser proxy settings to localhost:8080 and you can now listen to your music!

Okay, this next section is not for newbs. This is a script I wrote this morning to manage ssh port forwarding. It gives you the option of either:

1) Simply connecting to a shell,
2) Get a shell, but with port forwarding enabled,
3) No shell, just port forwarding, and runs as a daemon (in the background). This option also disables command execution for security reasons, because if you only need a proxy and not a shell to begin with, why risk it?

Enough explaining, here is the script:
###########################################
##  SSH Port-Forwarding Manager          ##
##  Author Chevis Young                  ##
###########################################
## Toggle SSH Port Fowarding:on, off, or ##
## as a silent daemon in the background. ##
###########################################

#/bin/bash
##Define Constants (ssh variables)  ##
SUSER=user     ##unix username
SHOST=host.example.com    ##remote host
SPORT=2222     ##ssh port
LPORT=8080     ##local port to forward
IDENTF=~/.ssh/id_rsa_whatever   ##identity file if needed

OPTIONS="Shell Proxy Daemon Quit"
select opt in $OPTIONS; do

if [ "$opt" = "Proxy" ]; then


echo "Shell with port forwarding requested, Set browser proxy settings to localhost:$LPORT socks 5"
ssh -i $IDENTF -p $SPORT -C -D $LPORT $SUSER@$SHOST

elif [ "$opt" = "Shell" ]; then

echo "No Port Forwarding Requested, executing shell..."
ssh -i $IDENTF -p $SPORT  $SUSER@$SHOST

elif [ "$opt" = "Daemon" ]; then
echo "Daemon mode requested, Set browser proxy settings to localhost:$LPORT socks 5"
ssh -i $IDENTF -p $SPORT -f -N -C -D $LPORT  $SUSER@$SHOST

elif [ "$opt" = "Quit" ]; then
echo Goodbye
exit


else
echo

echo -e "
  #################################################
  ## OPTIONS:                                    ##
  ## 1 Shell: Just give me a shell!              ##
  ## 2 Proxy: Shell+Port Forwarding on $LPORT.   ##
  ## 3 Daemon: No shell, just a proxy on $LPORT. ##
  ## 4 Quit!                                     ##
  #################################################"
fi
done

No comments:

Post a Comment