Skip to content

Instantly share code, notes, and snippets.

@terrywang
Last active February 28, 2016 13:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save terrywang/3983543 to your computer and use it in GitHub Desktop.
Save terrywang/3983543 to your computer and use it in GitHub Desktop.
SSH Tunnel
#!/bin/bash
# --------------------------------------
#
# Title: SSH Tunnel / Socks5 Proxy Script
# Author: Terry Wang
# Email: i (at) terry (dot) im
# Homepage: http://terry.im
# File: tunnel.sh
# Created: 1 Oct, 2012
#
# Purpose: Establishes/terminates a SSH Tunnel
#
# --------------------------------------
########### Setup SSH Proxy #############
# Set up SSH Host with Public Key Authentication
export MY_SSH_HOST=ubuntu@cloud.terry.im # username@host
export MY_PROXY_PORT=1080 #Socks5 port
############ End of Setup #############
if [ ! -f /tmp/.ssh_tunnel ]; then
echo "Establishing SSH Tunnel"
ssh -f -D $MY_PROXY_PORT $MY_SSH_HOST "if [ -f ~/.ssh_tunnel ]; then rm ~/.ssh_tunnel; fi; while [ ! -f ~/.ssh_tunnel ]; do echo > /dev/null; done" && touch /tmp/.ssh_tunnel
echo "SSH Tunnel established, socks5 proxy is available on port $MY_PROXY_PORT."
#You need libnotify to use notify-send
#notify-send -i /usr/share/icons/Human/scalable/categories/gnome-system.svg "HTTP Proxy" "Connected to the proxy"
else
echo "Terminating SSH Tunnel"
ssh $MY_SSH_HOST "touch ~/.ssh_tunnel"
rm /tmp/.ssh_tunnel
echo "SSH Tunnel terminated."
#You need libnotify to use notify-send
#notify-send -i /usr/share/icons/Human/scalable/categories/gnome-system.svg "HTTP Proxy" "Disconnected from the HTTP Proxy"
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment