Last active
December 3, 2019 14:16
-
-
Save matsadler/36b57feee6ad2f0ee957505af8ed8037 to your computer and use it in GitHub Desktop.
Bash script to setup/teardown SSH SOCKS proxy on macOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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