Skip to content

Instantly share code, notes, and snippets.

@jfear
Last active September 12, 2017 18:55
Show Gist options
  • Save jfear/546dcf9e05a9e234aa2d77658755e7f6 to your computer and use it in GitHub Desktop.
Save jfear/546dcf9e05a9e234aa2d77658755e7f6 to your computer and use it in GitHub Desktop.
A helper script for creating an ssh tunnel between machines for running jupyter notebook.
#!/bin/bash
# This is a little bash script to create a tunnel for running jupyter
# notebooks.
#
# You can run the script like so:
#
# $ jupyterTunnel start remote1
#
# This will create an ssh tunnel on port 7000 to remote 1, and create a control
# socket 'jupyter-ctrl-socket' in the current working direcotry. To stop the
# tunnel run (in the same direcotry as the control socket):
#
# $ jupyterTunnel stop remote1
#
# There is a third positional argument that allows you to set a different port
# number.
#
# $ jupyterTunnel start remote1 8888
#
if [ -n "$2" ]; then
host=$2
else
if [ -e ~/.jupyterTunnel_host ]; then
host=$(cat ~/.jupyterTunnel_host)
else
echo "You must provide a host argumnet."
exit 1
fi
fi
if [ -n "$3" ]; then
PORT=$3
else
PORT=7000
fi
if [[ $1 == 'start' ]]; then
ssh -M -S jupyter-ctrl-socket -fnNT -L $PORT:localhost:$PORT $host
echo $host > ~/.jupyterTunnel_host
elif [[ $1 == 'stop' ]]; then
ssh -S jupyter-ctrl-socket -O exit $host
rm ~/.jupyterTunnel_host
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment