Skip to content

Instantly share code, notes, and snippets.

@colinmollenhour
Created February 8, 2014 00:01
Show Gist options
  • Save colinmollenhour/8874471 to your computer and use it in GitHub Desktop.
Save colinmollenhour/8874471 to your computer and use it in GitHub Desktop.
Easily configure and launch your Tunnlr.com SSH tunnel!
#!/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