Skip to content

Instantly share code, notes, and snippets.

@rodmcnew
Created July 17, 2017 18:15
Show Gist options
  • Save rodmcnew/521549841e4c9199dd257254cdae313e to your computer and use it in GitHub Desktop.
Save rodmcnew/521549841e4c9199dd257254cdae313e to your computer and use it in GitHub Desktop.
socks-ssh-tunnel.bash
#Fill in these two vars and then set this script to run every minute in cron
REMOTE_HOST="somewhere.com"
LOCAL_PORT="12345"
#Leave this part alone
SSH_ARGS="$REMOTE_HOST -D $LOCAL_PORT -f -C -q -N"
START_COMMAND="ssh $SSH_ARGS"
echo ""
echo "Start command:"
echo $START_COMMAND
echo ""
echo 'Stop command:'
echo "pkill -f 'ssh $SSH_ARGS'"
echo ""
CHECK="ps -e | grep --quiet \"[ssh] $SSH_ARGS\""
if eval $CHECK; then
echo "The tunnel is already running on local port $LOCAL_PORT."
exit
fi
eval $START_COMMAND
echo "The tunnel has been started on local port $LOCAL_PORT."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment