Skip to content

Instantly share code, notes, and snippets.

Last active December 4, 2022 06:16
  • Star 18 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Automatic SOCKS proxy setup through SSH connection on Mac OS X
# Script for automatic setup of SOCKS proxy through SSH connection.
# It automatically teardowns SOCKS configuration before stopping.
# It's supposed to work on Mac OS X 10.6+
# Author: Adam Stankiewicz (@sheerun)
[[ -n "$1" ]] || { echo "Usage: proxy"; exit 1; }
# get service GUID and NAME (like Wi-Fi) to set SOCKS proxy
SERVICE_GUID=`printf "open\nget State:/Network/Global/IPv4\" | \
scutil | grep "PrimaryService" | awk '{print $3}'`
SERVICE_NAME=`printf "open\nget Setup:/Network/Service/$SERVICE_GUID\" |\
scutil | grep "UserDefinedName" | awk -F': ' '{print $2}'`
# command to disble socks proxy after at the end
finally() { sudo networksetup -setsocksfirewallproxystate "$SERVICE_NAME" off; }
# enable socks proxy in system configuration
sudo networksetup -setsocksfirewallproxy "$SERVICE_NAME" 1080 &&
ssh $1 -N -D 1080
trap finally INT
Copy link

markuz commented Mar 3, 2016

Tested in OS X 10.11.4, works perfectly ;-).

Copy link

lamiska commented Apr 12, 2016

Yep can confirm, works on osx 10.11 without problems, IP exclusion list in system settings is respected too

would be nice if script had -C option for compressed ssh connection on slow networks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment