Skip to content

Instantly share code, notes, and snippets.

@r-gr
Created October 10, 2013 16:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r-gr/6921086 to your computer and use it in GitHub Desktop.
Save r-gr/6921086 to your computer and use it in GitHub Desktop.
Simple ssh tunnel script. Tunnels all network traffic over ssh through desired server.
#! /bin/sh
# used with oh-my-zsh and placed in ~ZSH_CUSTOM/plugins/sshtunnel/sshtunnel.plugin.zsh
# must be enabled in .zshrc plugins e.g. plugins=(sshtunnel)
#
# both PORT and LOCAL_PORT must be replaced with desired values
# if tunneling over Wi-Fi needed, uncomment the 3 relevant lines
#
# oh, and closing the tunnel will kill all ssh processes
function _ssh_tunnel_help {
echo "usage: sshtunnel <command>\n"
echo " open Opens ssh tunnel to server and enables SOCKS proxy"
echo " close Closes ssh tunnel and disables SOCKS proxy"
echo " help Displays this message\n"
}
function sshtunnel {
if [ $1 ]
then
case "$1" in
open)
sudo -v
echo "enabling SOCKS proxy..."
# sudo networksetup -setsocksfirewallproxy Wi-Fi 127.0.0.1 LOCAL_PORT
sudo networksetup -setsocksfirewallproxy Ethernet 127.0.0.1 LOCAL_PORT
# sudo networksetup -setsocksfirewallproxystate Wi-Fi on
sudo networksetup -setsocksfirewallproxystate Ethernet on
echo "tunneling..."
ssh -f -D LOCAL_PORT -C -N user@server -p PORT
echo "tunnel opened."
;;
close)
sudo -v
echo "killing ssh processes..."
pkill ssh
pkill ssh-agent
echo "disabling SOCKS proxy..."
# sudo networksetup -setsocksfirewallproxystate Wi-Fi off
sudo networksetup -setsocksfirewallproxystate Ethernet off
echo "tunnel closed."
;;
*)
_ssh_tunnel_help
;;
esac
else
_ssh_tunnel_help
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment