Skip to content

Instantly share code, notes, and snippets.

@adamjmendoza
Forked from colinmollenhour/tunnlr.sh
Last active August 29, 2015 14:06
Show Gist options
  • Save adamjmendoza/faa6ad63535e01785ebf to your computer and use it in GitHub Desktop.
Save adamjmendoza/faa6ad63535e01785ebf to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# A simple function for starting an SSH tunnel (e.g. tunnlr.com)
#
# Add the function to your .bashrc file or use this script standalone
# Takes port to forward to as only parameter
function tunnlr(){
[ -z $1 ] && { echo "You must specify a port to forward to."; return 1; }
if [ -f ~/.tunnlr ]; then
source ~/.tunnlr
else
read -p "Enter your designated tunnlr.com port (e.g. 12951): " port
[ -n $port ] || return 1
read -p "Enter your designated tunnlr.com 'user@host': " host
[ -n $host ] || return 1
echo -e "port=$port\nhost=$host" > ~/.tunnlr
fi
[ -z $host -o -z $port ] && { echo "Invalid tunnlr config."; rm ~/.tunnlr; return 1; }
pid=$(pgrep -f "$host")
[ "$pid" = "" ] || { kill -9 "$pid" && echo "Killed existing tunnel."; }
if ssh -fNt -g -R :$port:0.0.0.0:$1 $host ; then
echo "Forwarding SSH port $port on $host to local port $1"
fi
}
# Ignore this line if pasting the above function in your .bashrc file
tunnlr $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment