Skip to content

Instantly share code, notes, and snippets.

@faizal2007
Last active September 4, 2015 17:21
Show Gist options
  • Save faizal2007/4b6bc3c24b4c137e8a4d to your computer and use it in GitHub Desktop.
Save faizal2007/4b6bc3c24b4c137e8a4d to your computer and use it in GitHub Desktop.
SSH Remote Port Forwarding
#!/bin/bash
##
#
# @package Localhost Port Forwarder
# Allow access private server behind firewall
#
# @todo
# + Server config save
# + Multiple server port
# + Switch port forward type L - Local Port R - Remote Port
# + Auto set cronjob to check service
##
#if [ "`ps -eaf | grep nNgf | grep $PORT | grep -v grep`" ] ; then
SERVER="localhost"
if [ "`ps -eaf | grep nNgf | grep -v grep`" ] ; then
echo "Local Port tunnel to $REMOTE is up"
else
echo -e "Enter ur access port "
read PORT
echo -e "Enter Port that need to be forward "
read PORTFORWARD
echo -e "Enter Remote Server IP "
read REMOTE
echo -e "Enter Remote Server Port "
read REMOTEPORT
echo -e "Enter Remote Server Username "
read REMOTEUSER
echo "Local Port tunnel ${REMOTE} NOT alive ... Restarting ..."
/usr/bin/ssh -nNgf -L $PORT:$SERVER:$PORTFORWARD $REMOTEUSER@$REMOTE -p$REMOTEPORT
logger -p daemon.notice "SSH Forward Local Port ${REMOTE} NOT Alive ... Restarting ..."
sleep 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment