Skip to content

Instantly share code, notes, and snippets.

@Fastidious
Forked from m3nt0r/proxy-toggle.sh
Last active June 3, 2016 18:04
Show Gist options
  • Save Fastidious/f9112bb22cc38c4e652f to your computer and use it in GitHub Desktop.
Save Fastidious/f9112bb22cc38c4e652f to your computer and use it in GitHub Desktop.
Starts/Stops an SSH Tunnel and SOCKS Proxy on OS X.
#!/bin/sh
SSH_HOST=user@host # Change this accordingly.
SSH_PORT=22 # If SSH server is listening on different port, change this.
PROXY_INTERFACE="Ethernet" # Change this to Wi-Fi, if you are on wireless, or to whatever your NIC is.
PROXY_HOST=localhost # This doesn't change.
PROXY_PORT=8080 # Pick a different port if you like.
if [[ $1 == "start" ]]; then
ssh -D $PROXY_PORT -f -C -q -N $SSH_HOST -p$SSH_PORT
sudo networksetup -setsocksfirewallproxy "$PROXY_INTERFACE" $PROXY_HOST $PROXY_PORT
echo "SSH Tunnel created and SOCKS Proxy Enabled."
echo "============================================================"
ps ax | grep $PROXY_PORT | head -n 1
echo "============================================================"
elif [[ $1 == "stop" ]]; then
ps aux | grep $SSH_HOST | grep -v grep | awk '{print $2}' | xargs kill -9
sudo networksetup -setsocksfirewallproxystate "$PROXY_INTERFACE" off
echo "SSH Tunnel stopped and SOCKS Proxy disabled."
elif [[ $1 == "status" ]]; then
echo "============================================================"
echo "Existing Network Services:"
echo "============================================================"
networksetup -listallnetworkservices
echo
echo "============================================================"
echo "Current SOCKS Proxy Settings:"
echo "============================================================"
networksetup -getsocksfirewallproxy "$PROXY_INTERFACE"
echo
echo "============================================================"
echo "Current SSH Tunnel Process:"
echo "============================================================"
process=`ps aux | grep $SSH_HOST | grep -v grep | awk '{print $2}'`
if [[ $process == "" ]]; then
echo "SSH Tunnel not started."
else
echo ">> [" $process "] <<"
fi
else
echo "`basename $0` creates an SSH Tunnel and toggles SOCKS proxy settings on OS X."
echo
echo "Usage: "
echo " $ sshtunnel start # Start SSH tunnel and SOCKS proxy."
echo " $ sshtunnel stop # Stops SSH tunnel and SOCKS proxy."
echo " $ sshtunnel status # Prints status information."
echo
echo "Proxy Interface: " $PROXY_INTERFACE
echo "Proxy Host: " $PROXY_HOST
echo "Proxy Port: " $PROXY_PORT
echo
exit 65 # End process with error to indicate incorrect arguments.
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment