Skip to content

Instantly share code, notes, and snippets.

@matsadler
Last active December 3, 2019 14:16
Show Gist options
  • Save matsadler/36b57feee6ad2f0ee957505af8ed8037 to your computer and use it in GitHub Desktop.
Save matsadler/36b57feee6ad2f0ee957505af8ed8037 to your computer and use it in GitHub Desktop.
Bash script to setup/teardown SSH SOCKS proxy on macOS
#!/bin/bash
if [[ -z "$1" ]]; then
echo "usage: $0 [user@]hostname"
exit 1
fi
ssh -ND 1080 "$1" &
PID=$!
INTERFACE=$(route get www.google.com | grep interface | awk '{ print $2 }')
SERVICE=$(networksetup -listnetworkserviceorder | grep "$INTERFACE" | awk -F ': |,' '{ print $2 }')
at_exit() {
networksetup -setsocksfirewallproxystate "$SERVICE" off;
ps -p "$PID" > /dev/null && kill -TERM "$PID"
}
trap at_exit EXIT
networksetup -setsocksfirewallproxy "$SERVICE" localhost 1080
wait "$PID"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment